Friday, 9 January 2009

Setting up a constant class

In my code I want to write Constant.Type.Value throughout  my code, well I’ve found a nice way to do this in C#.  All my constants are going sit in a single class and I can create static structures that hold the constant strings links so:

/// <summary>
/// This class holds all the constants for the Lynx application.
/// </summary>
public static class Constant
{
    /// <summary>
    /// Usage constants reference the rfUsage.Name field.
    /// </summary>
    public struct Usage
    {
        public const string Vendor = "Vendor";
        public const string Customer = "Customer";
        public const string ItemSize = "ItemSize";
        public const string Inventory = "Inventory";
    }
 
    /// <summary>
    /// Type contants reference the rfType.Name field
    /// </summary>
    public struct Type
    {
        public const string Weight = "Weight";
        public const string Thickness = "Thickness";
    }
 
    /// <summary>
    /// Unit of measure constants reference the rfUnitOnMeasure.Name field
    /// </summary>
    public struct UnitOfMeasure
    {
        public const string Grams = "Grams";
        public const string Millimeters = "Millimeters";
        public const string Box = "Box";
        public const string Bundle = "Bundle";
    }
}

And I make calls to the constants list like so:

Name = Constant.UnitOfMeasure.Grams

No comments: