views:

43

answers:

1

Is it OK to use Structs as Data library for harcoded values? Sometimes we can't avoid harcoding somethin althought its better to put it on something like xml file or a database table, but sometimes it's not possible for some reasons..

 public struct BatchStatus
 {
    public const string Submitted = "0901XX";
    public const string Active = "62783XY";
    public const string Inactive = "S23123";
 }

then I use it like this

 switch (batchStatus) // enums doesnt work in switch case
{
     case BatchStatus.Submitted:
         new Batch().Submit(); break;
    case BatchStatus.Inactive:
        new Batch1().Activate(); break;
    case BatchStatus.Active
        new Batch2().Deactivate(); break;

}
A: 

If you are using C# 2.0 and above, you should rather use a static class. Prior to C# 2.0, you can use a class an just add a private default constructor to ensure that the class in never instantiated.

C# 2.0 and later

public static class BatchStatus
{
  public const string Submitted = "0901XX";
  public const string Active = "62783XY";
  public const string Inactive = "S23123";
}

C# 1.0 - 1.2

public class BatchStatus
{
  public const string Submitted = "0901XX";
  public const string Active = "62783XY";
  public const string Inactive = "S23123";

  private BatchStatus()
  {

  }
}
Chris Taylor
is the static class still is a reference type? structs are value type and is saved in the stack meaning it is faster to locate than the class which are saved in the heap
CSharpNoob
Since the static class can never be instantiated and all the members of the class are static the fact that the type is a reference type is irrelevant since an instance of it will never exist anywhere. not on the stack or the heap.
Chris Taylor
thanks for clarification..
CSharpNoob
@CSharpNood, no problem, glad I could help.
Chris Taylor