I am using the following code inside a class:
When I go into a form and try to access it it wont work. Any ideas? (btw when i do global class it gives error....)Code:global int iTest = 0;
Printable View
I am using the following code inside a class:
When I go into a form and try to access it it wont work. Any ideas? (btw when i do global class it gives error....)Code:global int iTest = 0;
Take a look at the documentation for the global keyword.
You might want to look into making this variable a shared member of the appropriate class instead.
Fixed that for you. You take your stinkin' VB.NET terms back to the appropriate forum, thank you.
:p
So will making it static allow me to access it from different forms?
'static' allows you to access it from the class, rather than an instance of the class.
e.g. you can do Form1.MyVariable rather than needing to get a reference to the particular instance of Form1 that is being displayed.
It means also that all instances of Form1 will "share" that variable which can cause issues if you're not expecting it. If you have a reference to the instance of Form1, it is better to use an instance field rather than static for that reason.
To "access" the variable, it must also be visible to whatever is trying to use it. This is where public/protected/internal/private come in. In terms of members in a class (both static and instance, applies to fields, properties and methods) "public" means anyone can access the variable, "protected" means any derived classes can access the field, "internal" means any classes in the same assembly or that the containing assembly declares as InternalsVisibleTo (assembly attribute, usually in Properties.cs) can access it (you can combine protected and internal although note this is a logical OR, not a logical AND) and "private" means it is only accessible within the containing class.
OOOOOOOHHHH so thats what static does!!!! Legit! Thanks Evil =D