471,338 Members | 1,044 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Enum or Struct Help: Working with String Values

Hi There

I have some codes that represent Sale Types i.e. A = On Account, C = Cash, D
= Debtor, V = Voucher

I want to create an enum or struct to work with the logical names like
"Cash" as opposed to "C"

I see the enums cannot work with string values like
public enum SaleType
{
OnAccount = "A",
Cash = "C"
}

Is there another way to do this, maybe with structs? Yet structs seem to
need Constructors to set the public properties/public variables.

Any tips appreciated,

Chev

Jun 27 '08 #1
3 1516
"Chevron Boyde" <sa****@buzzjuicebars.comwrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
Hi There

I have some codes that represent Sale Types i.e. A = On Account, C = Cash,
D
= Debtor, V = Voucher

I want to create an enum or struct to work with the logical names like
"Cash" as opposed to "C"

I see the enums cannot work with string values like
public enum SaleType
{
OnAccount = "A",
Cash = "C"
}

Is there another way to do this, maybe with structs? Yet structs seem to
need Constructors to set the public properties/public variables.
Here is a simplistic solution:-

public static class SaleType
{
public static readonly string OnAccount = "A";
public static readonly string Cash = "C";
}

However you can't use this to type a variable as you would an enum

IOW this is an error:-

SaleType x = SaleType.Cash

it would need to be:-

string x = SaleType.Cash
--
Anthony Jones - MVP ASP/ASP.NET
Jun 27 '08 #2
"Chevron Boyde" <sa****@buzzjuicebars.comwrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
Hi There

I have some codes that represent Sale Types i.e. A = On Account, C = Cash,
D = Debtor, V = Voucher

I want to create an enum or struct to work with the logical names like
"Cash" as opposed to "C"

I see the enums cannot work with string values like
public enum SaleType
{
OnAccount = "A",
Cash = "C"
}

Is there another way to do this, maybe with structs? Yet structs seem to
need Constructors to set the public properties/public variables.

Any tips appreciated,

Chev
Maybe you could use the codes as the enum elements then use the
[Description] attribute on the elements of the enum to get meaningful names:

using System;
using System.ComponentModel;

public enum SaleType
{
[Description("On Account")]
A,
[Description("Cash")]
C,
[Description("Debtor")]
D,
[Description("Voucher")]
V
}

I have a class in my toolbox (which admittedly I 'found' somewhere) which
can then return the description if one has been defined:

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

namespace MyCompanyUtils
{
public static class EnumInfo
{
public static string GetElementDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length 0) ? attributes[0].Description :
value.ToString();
}
}
}

class Program
{
static void Main(string[] args)
{
SaleType st = SaleType.A;
Console.WriteLine(EnumInfo.GetElementDescription(s t));
// Output: On Account
}
}
Hope this helps.

David
Jun 27 '08 #3
Thanks Tony!

"Anthony Jones" <An*@yadayadayada.comwrote in message
news:Oz**************@TK2MSFTNGP06.phx.gbl...
"Chevron Boyde" <sa****@buzzjuicebars.comwrote in message
news:ew**************@TK2MSFTNGP02.phx.gbl...
>Hi There

I have some codes that represent Sale Types i.e. A = On Account, C =
Cash,
D
>= Debtor, V = Voucher

I want to create an enum or struct to work with the logical names like
"Cash" as opposed to "C"

I see the enums cannot work with string values like
public enum SaleType
{
OnAccount = "A",
Cash = "C"
}

Is there another way to do this, maybe with structs? Yet structs seem to
need Constructors to set the public properties/public variables.

Here is a simplistic solution:-

public static class SaleType
{
public static readonly string OnAccount = "A";
public static readonly string Cash = "C";
}

However you can't use this to type a variable as you would an enum

IOW this is an error:-

SaleType x = SaleType.Cash

it would need to be:-

string x = SaleType.Cash
--
Anthony Jones - MVP ASP/ASP.NET

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Derek | last post: by
reply views Thread by Patrick Guio | last post: by
4 posts views Thread by Angus Comber | last post: by
10 posts views Thread by James Brown | last post: by
21 posts views Thread by Andreas Huber | last post: by
9 posts views Thread by Lawrence Oluyede | last post: by
6 posts views Thread by Jason Larion | last post: by
34 posts views Thread by Steven Nagy | last post: by
12 posts views Thread by Cmtk Software | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.