Results 1 to 6 of 6

Thread: [RESOLVED] (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Resolved [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....)
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: (C#.NET)It won't let me use globals, or it will but it doesn't do what it should!

    Quote Originally Posted by Atheist View Post
    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.


  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    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
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width