
- VBForums
- .NET and More
- C#
- [RESOLVED] (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
-
Jun 27th, 2011, 12:17 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
I am using the following code inside a class:
Code:
global int iTest = 0;
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....)
-
Jun 27th, 2011, 12:22 AM
#2
Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
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.
-
Jun 27th, 2011, 01:40 AM
#3
Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
 Originally Posted by Atheist
You might want to look into making this variable a static member of the appropriate class instead.
Fixed that for you. You take your stinkin' VB.NET terms back to the appropriate forum, thank you.
-
Jun 27th, 2011, 04:17 PM
#4
Thread Starter
Fanatic Member
Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
So will making it static allow me to access it from different forms?
-
Jun 27th, 2011, 06:06 PM
#5
Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
'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.
-
Jun 28th, 2011, 02:10 AM
#6
Thread Starter
Fanatic Member
Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
OOOOOOOHHHH so thats what static does!!!! Legit! Thanks Evil =D

- VBForums
- .NET and More
- C#
- [RESOLVED] (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|