Creating a global variable how to ?

C

Carlos

Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program but
how can I do that inc C# ?

Thanks
 
T

The Crow

public sealed class ApplicationVariables
{
private ApplicationVariables()
{
}

private static string stringVariable;
public static string StringVariable
{
get{return stringVariable;}
set{stringVariable = value;}
}
}

if its readonly you gain a bit performance by marking readonly. for example
:

public sealed class ApplicationVariables
{
private ApplicationVariables()
{
}

private static readonly string stringVariable =
System.Configuration.ConfigurationSettingsAppSettings["StringVariable"];
public static string StringVariable
{
get{return stringVariable;}
}
}

compiler will optimise this.
 
S

Siva M

Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program but
how can I do that inc C# ?

Thanks
 
C

Carlos

Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my public
class ?
 
S

Siva M

Did you declare you variable as public? Here is an example (both in the same
namespace):

public class MyClass
{
public int MyIntVar = 0;
public static string MyMsg = "Hello";
}

public class AnotherClass
{
public void AnotherClassMethod()
{
MyClass m = new MyClass();
Console.WriteLine (m.MyIntVar);
Console.WriteLine (MyClass.MyMsg);
}
}

If the variables are public and static then they can be accessed without
instantiating the containing class (MyMsg in the above example).

Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my public
class ?
 
S

Siva M

Yes, each instance get a new value. If you need to retain values, then go
for static variables, which can be accessed without instantiation.

Yes, but when you do a new do not you loose the previous value ?
 
T

The Crow

siva, marking class sealed prevents inheriting from that class and causes
some compiler optimizations. providing private constructor prevents this
class to be instantinated as all methods - members will be static. expanding
fields to outside of the class making the field public is not a good
practice. encapsulating with properties is better. if properties just
returns-sets specific fields, compiler is clever enough to give access to
field directly behind the scenes..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top