file of constants? (vs2005/c#)

  • Thread starter Thread starter MarkusR
  • Start date Start date
M

MarkusR

Good day, bare with me while I probably ask a very novice question...

I have multiple projects that use the same constants. I want to create
a common file they can share.

Is this the best way to do it:

using System;

namespace myconsts
{
class orderconsts
{
public const int ORDER_STATUS_HOLD = 1;
public const int ORDER_STATUS_ERROR = 2;
public const int ORDER_STATUS_READYTOSHIP = 3;
public const int ORDER_STATUS_PACKAGED = 4;
public const int ORDER_STATUS_SHIPPED = 5;
public const int ORDER_STATUS_VOIDED = 6;
}
}

and access it using orderconsts.ORDER_STATUS_HOLD

-Markus_R
 
First, I'd think this would be an enum. Second, you'd probably want to
embed this in your Order class, which knows about its own status.
Other projects which need to know order status would likely ask this
order class.

HTH
Andy
 
Hey Andy,

Actually you are probably right. I forget to use enums. (consts always
start out with 1 or 2 and grow).

This project that I am doing is extremely small and I opted to not use
OO for everything. About 80 lines of code. But, again, another good
point. I will have to think if I could organize all into a reusable
class.

-Markus_R
 
I do mine like this:



public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
[Description("Items of this enum are the coolest")] SuperCool =
1
}

Ok.. then I have a EnumHelper.


using System;
using System.ComponentModel;
using System.Reflection;
using System.Xml;

namespace MyCompany.Enums
{
/// <summary>
/// Summary description for EnumHelper.
/// </summary>
public class EnumHelper
{
private EnumHelper()
{
//only static methods
}

public static string GetDescription(System.Enum value)
{

//if a description is defined for an enum value, this procedure will get
it

//Example of defining a description with an enum value
/*
*

public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
}


*
* */

FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),
false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();

}

}


I kinda get the best of both worlds.
I use the enums as enums 99% of the time.

But I have get a long description if I ever need it.
 
Thanks much sloan. I will have to study this. ;)

-Markus
I do mine like this:



public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
[Description("Items of this enum are the coolest")] SuperCool =
1
}

Ok.. then I have a EnumHelper.


using System;
using System.ComponentModel;
using System.Reflection;
using System.Xml;

namespace MyCompany.Enums
{
/// <summary>
/// Summary description for EnumHelper.
/// </summary>
public class EnumHelper
{
private EnumHelper()
{
//only static methods
}

public static string GetDescription(System.Enum value)
{

//if a description is defined for an enum value, this procedure will get
it

//Example of defining a description with an enum value
/*
*

public enum ExampleEnum
{
[Description("MyUnknownDescription")] Unknown = 0
}


*
* */

FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute),
false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();

}

}


I kinda get the best of both worlds.
I use the enums as enums 99% of the time.

But I have get a long description if I ever need it.




MarkusR said:
Good day, bare with me while I probably ask a very novice question...

I have multiple projects that use the same constants. I want to create
a common file they can share.

Is this the best way to do it:

using System;

namespace myconsts
{
class orderconsts
{
public const int ORDER_STATUS_HOLD = 1;
public const int ORDER_STATUS_ERROR = 2;
public const int ORDER_STATUS_READYTOSHIP = 3;
public const int ORDER_STATUS_PACKAGED = 4;
public const int ORDER_STATUS_SHIPPED = 5;
public const int ORDER_STATUS_VOIDED = 6;
}
}

and access it using orderconsts.ORDER_STATUS_HOLD

-Markus_R
 
Back
Top