473,323 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 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 1598
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Derek | last post by:
A simple struct struct Foo { float x; char y; }; The above class occupies 8 bytes on my Win32 system according to sizeof(Foo). This makes sense because of the platform's alignment...
0
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir...
4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
9
by: Lawrence Oluyede | last post by:
I have a list of strings and i'd like to build up an enum from them... is there a way to do that? Thanks in advance. -- Lawrence "Rhymes" Oluyede http://loluyede.blogspot.com
6
by: Jason Larion | last post by:
When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
12
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.