Preserving a Class instance after resetting the Project

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,806
Office Version
  1. 2016
Platform
  1. Windows
I instatiate a class Object and then I reset the vb project .Obviously, the result is that the Class instance is destroyed.

Is there a workaround this so I can preserve the Class instance even after resetting the vb project?

Code:
Private oClass1 As Class1

Sub InstantiateClass()

    Set oClass1 = New Class1
    oClass1.SomeProperty = "bla bla"
    
    MsgBox oClass1.SomeProperty

End Sub
After resetting the Project the code below errors out :

Code:
Sub MacroAFterResettingtheProject()

    MsgBox oClass1.SomeProperty

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hello Jaafar,

Have you tried exporting the Class Module and then importing back into workbook when needed?

Sincerely,
Leith Ross
 
Upvote 0
Hello Jaafar,

Have you tried exporting the Class Module and then importing back into workbook when needed?

Sincerely,
Leith Ross

Thanks Leith.

That woudn't solve the loss of state problem. I was refering to preserving the instance of the Class module and its current Properties as opposed to the Class module itself.
 
Upvote 0
Hello Jaafar,

Sorry, I didn't know you were referring to the state of the Class properties. As you mentioned, all Classes in VBA are single instance and as such are destroyed when they go out of scope. It would not be a problem if you created multiple instances like a mutex object. To do that would require going outside VBA. Perhaps you could link through a C++ dll, or something similar. Although to me, that seems to be lot of work to get around this problem.

Sincerely,
Leith Ross
 
Upvote 0
You could maybe implement a singleton pattern on the class instance.
It will still be destroyed when you reset the project, but your code could then call the singleton getter function instead of operating directly on the object, which would then recreate the object if it doesn't exist at the time of calling. That way your code will always function, also after a project reset.

That would of course not solve the problem that you lose the actual state of the destroyed instance, and get a 'fresh' initialised state. It is not clear if that is a problem or not in your case...
 
Upvote 0
Leith,

Sorry, I didn't know you were referring to the state of the Class properties. As you mentioned, all Classes in VBA are single instance and as such are destroyed when they go out of scope. It would not be a problem if you created multiple instances like a mutex object. To do that would require going outside VBA. Perhaps you could link through a C++ dll, or something similar. Although to me, that seems to be lot of work to get around this problem.
I thought about something similar like adding a reference to a memory location in an outside process or better still, locking the memory location pointing to the Class instance and then retrieving it by using the CopyMemory API function but i am still testing.



Hermanito,

You could maybe implement a singleton pattern on the class instance.
It will still be destroyed when you reset the project, but your code could then call the singleton getter function instead of operating directly on the object, which would then recreate the object if it doesn't exist at the time of calling. That way your code will always function, also after a project reset.
I am not sure what you mean by the singleton pattern.
Is is like saving the Object's Properties somewhere like in a text file or in a Cell etc.. and then recreate a new calss object and adding to it the saved Properties ?

Thank you.
 
Upvote 0
You can find plenty of info on the singleton pattern by googling it... it is a very common design pattern.
Basically it is a way to ensure you have one single instance of a certain class, and one instance only. No matter when and where you use the instance in code, you always get that same one instance back and in case it is not yet instantiated, it does that for you.

What is causing you to need to keep the instance, even when resetting the project?

If you have state info you need to keep, even after a reset, you will need to store that info somewhere, to put it on a worksheet seems like a logical choice.
To find more info on that, you can google for 'object persistence', which is the commonly used term for this process...
 
Upvote 0
You can find plenty of info on the singleton pattern by googling it... it is a very common design pattern.
Basically it is a way to ensure you have one single instance of a certain class, and one instance only. No matter when and where you use the instance in code, you always get that same one instance back and in case it is not yet instantiated, it does that for you.

What is causing you to need to keep the instance, even when resetting the project?

If you have state info you need to keep, even after a reset, you will need to store that info somewhere, to put it on a worksheet seems like a logical choice.
To find more info on that, you can google for 'object persistence', which is the commonly used term for this process...


Thanks Hermanito.

I am just doing this out of interest really. I'll try googling the "singleton pattern" and see what is is. As for storing the Class instance Properties somewhere like in a cell or text file I have done it before along with the use of UDTs to make it easier to handle. However, I was looking for an alternative .
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,181
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top