Emad Khan
Emad Khan

Reputation: 51

How can I use static globally defined variables in c#?

Say I have few STATIC Defined column codes around 5 and I wish to use them in several files not by using "" strings but by calling them for example :

I wish I would have a class in where my codes would be stored and whenever I call them they are called by their values for example:

public static class ColumnCodes {
 EstimatedDelivery = "ED";
 ActualDelivery = "AD";
}

and when I call them in any other class it would be:

public void x{
  var a = ColumnCodes.EstimatedDelivery; 
  // so this would be compiled like var a = "ED";
}

Upvotes: 3

Views: 84

Answers (2)

Using the reserved word CONST.

public static class ColumnCodes 
{
   public const string EstimatedDelivery = "ED";
   public const string ActualDelivery = "AD";
}

Upvotes: 4

Alejandro
Alejandro

Reputation: 413

You still need to define a type for the static values and if you are not planning to change them since they are static, might as well make them readonly values so other classes don't change it at any point.

public static class ColumnCodes {
   public static readonly string  EstimatedDelivery = "ED";
   public static readonly string  ActualDelivery = "AD";
}

Upvotes: 7

Related Questions