James Strater


Excel 2003 VBA: I have a class module that needs to refer to a global variable, but I don't want to force every project that uses the class to declare such a variable - the functionality here is optional, yet I need the variable to persist. I also don't want to force projects that use this class to globally declare instances of this class, there are already quite a few projects that use the class and they all use locally defined instances of the class within procedures.

So I'm kind of screwed, especially since this variable is just a string. I have tried to create a property of my class where I can set this string by reference, but this doesn't do what I want:

Private msNote as String

Public Property Set NoteString(ByRef sNote As String)

msNote = sNote

End Property

What I really want is (pseudo C/C++/VBA mix):

Private msNote as *String

Public Property Set NoteString(ByRef sNote As String)

msNote = *sNote

End Property

So how do I set a class module variable to equal a reference to this string, so that when the string gets changed inside the class it also changes the global string In C/C++ this would be easy, just set a pointer variable equal to the address of sNote. But VBA doesn't really have that ability, or does it Is there way to set a module variable equal to this ByRef variable inside Class_Initialize and have it really be a reference to the same global string I'm kind of doubting it, but I thought I'd ask.

I know that I could wrap this string in an object in this way, but I'd rather not have to:

Private myNote as Object

Private Sub Class_Initialize(Optional ByRef Note As Object)


Set myNote = Note

End Sub

Except that even this requires that I create a new class for my object or do some kind of kludgy thing where I use a Collection, which seems like the only read-made class that's simple enough. Any other ideas

Thanks,

James




Re: Reference to a String

Adamus Turner


Global variable I don't support globals but they are handy on occasion.

Adamus