Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#VB.NET
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 10-Mar-11 20:32pm
Edited 28-Nov-11 21:51pm
v3
Comments
Dalek Dave at 11-Mar-11 3:41am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
SAKryukov at 11-Mar-11 3:11am
   
Sure, 5, and good point about global. --SA
Dalek Dave at 11-Mar-11 3:41am
   
Spot on!
Nuri Ismail at 11-Mar-11 3:45am
   
Very good answer. 5
Dalek Dave at 29-Nov-11 4:05am
   
Tick VG have a 5!
RaisKazi at 29-Nov-11 9:08am
   
Agree with the Answer. My 5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Dalek Dave at 11-Mar-11 3:41am
   
Good example and explanation.
SAKryukov at 11-Mar-11 10:57am
   
Thank you, Dalek, --SA
Nuri Ismail at 11-Mar-11 3:47am
   
Very good answer. My 5.
SAKryukov at 11-Mar-11 10:58am
   
Thank you, Nuri, --SA
RaisKazi at 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).
SAKryukov at 29-Nov-11 11:17am
   
You are right: as far as I know Module is a static class in VB.NET (I never use VB, can be wrong). Thank you for the note. --SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

public static String VariableName;
//write in program file
  Permalink  
Comments
Dalek Dave at 11-Mar-11 3:41am
   
True
RaisKazi at 29-Nov-11 9:12am
   
Voted 4. I think Static in C# is closer to Module in VB.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

create class in project and declare the variable in class.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 460
1 Sergey Alexandrovich Kryukov 336
2 Shai Vashdi 294
3 OriginalGriff 280
4 Abhinav S 160
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 29 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid