473,243 Members | 1,645 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,243 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 1594
"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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...

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.