Enumeration and Description

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

If I have an Enumeration can I give a text description for each value?

e.g. something like
Public Enum Colors

Black "Really cool Black"

Blue "Ice Cold Labatts"

End Enum


If so, how do I get the text description for each value?
Code snippet would be great.
Thanks!
 
jfallon1 said:
If I have an Enumeration can I give a text description for each value?

e.g. something like
Public Enum Colors

Black "Really cool Black"

Blue "Ice Cold Labatts"

End Enum


If so, how do I get the text description for each value?
Code snippet would be great.

This link shows how to set the description as well as how to retrieve it
programatically:

http://tinyurl.com/3yc4o
 
Joe Fallon said:
If I have an Enumeration can I give a text description for each
value?

e.g. something like
Public Enum Colors

Black "Really cool Black"

Blue "Ice Cold Labatts"

End Enum


If so, how do I get the text description for each value?
Code snippet would be great.

No, not possible. You could create a class containing a short and long
description, like:

public class mycolor
public readonly Name as string
public readonly Description as string
public sub new(name as string,description as string)
me.name = name
me.description = description
end sub
end class

Optionally/in addition:

public class mycolor
public readonly Name as string
public readonly Description as string
private sub new(name as string,description as string)
me.name = name
me.description = description
end sub
public shared readonly Blue as new mycolor _
("Blue", "Ice Cold Labatts")
end class


- or an arraylist with key=name and value=description, or...


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Thanks!
Here is a more general version:
Public Shared Function GetDescription(ByVal value As [Enum]) As String

Dim fi As FieldInfo = value.GetType().GetField(value.ToString())

Dim attributes As DescriptionAttribute() =
CType(fi.GetCustomAttributes(GetType(DescriptionAttribute), False),
DescriptionAttribute())

If attributes.Length > 0 Then

Return attributes(0).Description()

Else

Return value.ToString()

End If

End Function
 
Joe Fallon said:
If I have an Enumeration can I give a text description for each value?

As I see things, the main point of using Enumerations is to make the
"values" within them more meaningful in their own right - self-describing,
if you like - and less like the "Magic Numbers" that we all know and
love ;-)

So, if you want to give your colours names, go for it ...

Public Enum Colours
ReallyCoolBlack = &H0
IceColdLabatts = &HFF0000
. . .

(OK, they're VB6 colour codes - I haven't got to the .Net ones yet).

HTH,
Phill W.
 
Back
Top