Scope in VB

Started by
5 comments, last by jbadams 19 years, 5 months ago
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
Advertisement
I'm not an expert in vb, but I have done a bit. I think you need to put global before the variable.
www.stickskate.com -> check it out, some gnarly stick skating movies
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"
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).

- Jason Astle-Adams

If the values won't change, then you can put them in an Enum:

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.
It works with the public keyword, thanks to you all :)
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.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement