Jump to content
  • Advertisement
Sign in to follow this  
Quak

Scope in VB

This topic is 5058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!