Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

I am a little confused with C#.NET.

When I develop an app using VB.NET, I can use Module.
I declare public variable in this module and access it from all part of the project.

But, when I change to C#.Net, there is no module in C#.NET.

How should I proceed?

How do I declare Global Variable in C#.NET?

Please Help...
Posted
Updated 28-Nov-11 21:51pm
v3
Comments
Dalek Dave 11-Mar-11 3:41am    
Edited for Grammar and Readability.

C# has no concept of global variables: everything is class scope.
However (just like VB) you can declare a static class and access everything in it as if they were global:
public static class Globals
   {
   public static int MyGlobalValue = 42;
   }

...


private void MyMethodSomewhereInAClass()
   {
   Console.WriteLine(Globals.MyGlobalValue);
   MyGlobalValue += 23;
   }
Technically, the class does not need to be static, but since you should not need to instatiate it, it is a good idea.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Mar-11 3:11am    
Sure, 5, and good point about global.
--SA
Dalek Dave 11-Mar-11 3:41am    
Spot on!
Nuri Ismail 11-Mar-11 3:45am    
Very good answer. 5
Dalek Dave 29-Nov-11 4:05am    
Tick VG have a 5!
RaisKazi 29-Nov-11 9:08am    
Agree with the Answer. My 5.
class MyClass {
    public int MemberOne;
    internal int MemberTo = 13; //can initialize
    internal protected string MemberThree;
    private System.DataTime Time; //can omit "private": default for class/struct
    //property, one of several forms of declaration:
    public int MemberFour { get; set; } 
}


In intentionally listed all access modifiers: learn them all.
As a rule of thumb, public fields should be discouraged and properties preferred.

Now, asking questions like that will waste anyone's time and get you nowhere. First, read a manual and do exercises. Read my non-nonsense instructions:
I have a problem with my program. Please help![^].

Don't forget this: What are Apache Struts and JBoss Hibernate[^]. (Ignore the topic, read my Answer.)

This might open your eyes: http://norvig.com/21-days.html[^] (how much time do you need?).

Good luck.
—SA
 
Share this answer
 
v2
Comments
Dalek Dave 11-Mar-11 3:41am    
Good example and explanation.
Sergey Alexandrovich Kryukov 11-Mar-11 10:57am    
Thank you, Dalek,
--SA
Nuri Ismail 11-Mar-11 3:47am    
Very good answer. My 5.
Sergey Alexandrovich Kryukov 11-Mar-11 10:58am    
Thank you, Nuri,
--SA
RaisKazi 29-Nov-11 9:11am    
Voted 4 for the information. But I think Static in C# is closer to Module in VB(Griff's Answer covers this).
public static String VariableName;
//write in program file
 
Share this answer
 
Comments
Dalek Dave 11-Mar-11 3:41am    
True
RaisKazi 29-Nov-11 9:12am    
Voted 4. I think Static in C# is closer to Module in VB.
create class in project and declare the variable in class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900