Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Shared" classes in VB6...

Status
Not open for further replies.

MattWoberts

Programmer
Oct 12, 2001
156
GB
Hi,

I need to implement a class in VB6 that is contains "static" variables. Now, in VB.NET, these would be "shared":

Public class class1
public shared X as integer
End Class

With this code I can access Class1.X without instantiating a class1:
class1.X = 2

Can anyone please tell me if/how I can do the same in VB6?

Thanks!


 
It's not very encapsulated, but I've written classes that used global variables declared in a module--that might be the only work around for this in VB. Maybe a global array/collection to handle multiple instantiations of the class?
 
To be honest, I'm not convinced that .NETs public shared variables are a great idea. But it is possible to get very, very similar in VB6 if you really, really want to...
 
VB6 doesn't really have the concept of shared/static class members. The closest you could come would be to use a global variable in a .bas module.

strongm -
Shared variables have their uses, but sometimes they get overused. Part of the usual "shiney new toy" scenario. Remember Active Documents in VB4?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Here's how to do it in VB6

1. Start a new ActiveX Exe or DLL
2. Add the following code to the class:
Code:
Option Explicit

Public Wombat As Long

Set the instancing of the class to 5 - GlobalMultiuse

Ok, we're done with our class.

Now start a new project, and add a reference to the class we've just created. And a command button.

In my example the Class is called Class1, and lives in an ActiveX DLL called GlobalDLL. The following code works much like .NET's shared variables in a particular scope:

Option Explicit

Private Sub Command1_Click()
GlobalDLL.Wombat = 4
Debug.Print GlobalDLL.Wombat
End Sub
 
Thanks strongm!

Thats a good tip...I had looked on the web and all I had found was the module workaround as chip said.

I'm coming from a c++ background - I find shared variables really useful in an OO world. One final thing - will setting the instancing to multiuse upset COM in any way? I will be referencing my "GlobalDLL" from COM dll's....

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top