Click here to Skip to main content
12,501,486 members (48,373 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VB 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
dartfrog1.1K
Updated 28-Nov-11 21:51pm
v3
Comments
Dalek Dave 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 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.
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 11-Mar-11 3:41am
   
Good example and explanation.
SAKryukov 11-Mar-11 10:57am
   
Thank you, Dalek,
--SA
Nuri Ismail 11-Mar-11 3:47am
   
Very good answer. My 5.
SAKryukov 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).
SAKryukov 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 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.
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 29 Nov 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100