Enum or Struct Help: Working with String Values

C

Chevron Boyde

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
 
A

Anthony Jones

Chevron Boyde said:
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
 
D

Dave Shooter

Chevron Boyde said:
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(st));
// Output: On Account
}
}


Hope this helps.

David
 
C

Chevron Boyde

Thanks Tony!

Anthony Jones said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top