Scope in VB
Hi guys,
how can I use the same global variables in different forms of the project in VB? Where do I have to declare them ?
Thanks
Quak
I'm not an expert in vb, but I have done a bit. I think you need to put global before the variable.
when I put it in front of the declaration a compile error says:
"Constants, Fixed-Length Strings, bla bla not allowed as public members of object modules"
"Constants, Fixed-Length Strings, bla bla not allowed as public members of object modules"
You need to declare it in a module (ie. not actually in a forms code at all) if you're using Global. You could also declare the variable as Public, and use form.variable (or module.variable) to access it.
(I assumed VB6 btw).
(I assumed VB6 btw).
If the values won't change, then you can put them in an Enum:
Class module MyClass:
Otherwise there is no way to put a global variable into a class. You can declare globals in standard modules, but that will make your classes non-portable. You would have to make sure that you include that standard module in any project where you're using class modules that access the variable.
Class module MyClass:
Public Enum CrazyValues VALUE1 = 1 VALUE2 = 2End Enum
Otherwise there is no way to put a global variable into a class. You can declare globals in standard modules, but that will make your classes non-portable. You would have to make sure that you include that standard module in any project where you're using class modules that access the variable.
No problem. You shouldn't really need Global anyway. But now that you've got it working with Public variables, don't overuse them - only make things Public if they need to be, it helps to prevent bugs, and keeps your code cleaner.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement