VB6 doesn't support "Shared" but you can declare a Public variable in a module and it will be visible everywhere in your app ... including instances of a class.
Risky of course because it's also visible in code that's not part of the class where you intend to use it. The other bad thing about this is that your classes are not as portable as they should be because they depend on the existence of a Public variable that must be defined outside the class in order to operate.
Another option is a wrapper class something like this
Code:
Dim mvarShared As Long
Public Property Let Shared(vData As Long)
mvarShared = vData
End Property
Public Function NewInstance() As TheClass
Dim NInstance As TheClass
Set NInstance = New TheClass
NInstance.Shared = mvarShared
Set NewInstance = NInstance
Set NInstance = Nothing
End Function
And "TheClass" would contain
Code:
Private mvarShared As Long
Public Property Let Shared(vData As Long)
mvarShared = vData
End Property
Public Property Get Shared() As Long
Shared = mvarShared
End Property
[COLOR=black cyan]' Other Properties, Methods, Etc.[/color]
and you would use it as
Code:
Dim myWrapper As New WrapperClass
Dim FirstInstance As myClass
Dim SecondInstance As myClass
myWrapper.Shared = 6789
Set FirstInstance = myWrapper.NewInstance
Set SecondInstance = myWrapper.NewInstance
Debug.Print FirstInstance.Shared
Debug.Print SecondInstance.Shared
You can probably clean this up considerably. It's just "speculative" code. I haven't tested it.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.