keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp > 07/2008

 #1  
07-02-08, 09:57 AM
nomad
Hi,

I have an enum below which I need to be able to reference by passing
in "Insured Only". When I do this it says that it cannot be found as
the actual enum shows as InsuredOnly. Is there anyway I can pass in
the correct value i.e. "Insured Only" instead of InsuredOnly?

Appreciate any help on this.


[System.Runtime.Serialization.EnumMemberAttribute(V alue="Insured
Only")]
InsuredOnly = 0,

[System.Runtime.Serialization.EnumMemberAttribute(V alue="Insured and
Spouse")]
InsuredandSpouse = 1,
 #2  
07-02-08, 11:14 AM
Marc Gravell
How are you passing it in? For example, XmlSerializer (and a few
others) respect XmlEnumAttribute.
Enum.Parse only uses the actual value, but you can use reflection to
roll your own parse routine that looks at some attribute. I've seen
DescriptionAttribute used in this way many times...

Marc
 #3  
07-02-08, 11:53 AM
nomad
On 2 Jul, 11:14, Marc Gravell <marcgrav> wrote:
> How are you passing it in? For example, XmlSerializer (and a few
> others) respect XmlEnumAttribute.
> Enum.Parse only uses the actual value, but you can use reflection to
> roll your own parse routine that looks at some attribute. I've seen
> DescriptionAttribute used in this way many times...
>
> Marc


Hi Marc,

Thanks for your reply.

I am passing it in like below. classofUse[0] = "Insured Only" while
the enum is "InsuredOnly". I have read about refraction but not sure
how I can do it as per my example below. Really appreciate any
suggestions/code snippets.

Description =
(VehicleUsageDescription)Enum.Parse(typeof(Vehicle UsageDescription),
classofUse[0])
 #4  
07-02-08, 12:22 PM
Marc Gravell
Here's an example; it isn't the slickest I've seen (personally I'd use
generics a bit more), but:

http://blog.waynehartman.com/articles/84.aspx

Marc
 #5  
07-02-08, 01:29 PM
nomad
On Jul 2, 12:22 pm, Marc Gravell <marcgrav> wrote:
> Here's an example; it isn't the slickest I've seen (personally I'd use
> generics a bit more), but:
>
> [..]
>
> Marc


Marc,

Thank you very much for the link; I had to make some amendments to get
it to work how I needed it but it was then spot on.

Thanks again

All times are GMT. The time now is 09:46 AM. | Privacy Policy