Sign in to follow this  
Quak

Scope in VB

Recommended Posts

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

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
If the values won't change, then you can put them in an Enum:

Class module MyClass:

Public Enum CrazyValues
VALUE1 = 1
VALUE2 = 2
End 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this