Results 1 to 8 of 8

Thread: Enum to represent String constants

Threaded View

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Enum to represent String constants

    Enumerations are basically used to represent numeric constants. But many a times I have felt the need of my enumerations to represent string constants.
    Here is one way of how to make this possible. This makes use of the capabilities of the System.ComponentModel namespace.

    This is how you define the enum:
    (Notice the Description metadata attribute contains the string value we want for the enum)

    vb.net Code:
    1. ''Put the string value of your enum in the Description metatag.
    2. '' (Dont forget to Import System.ComponentModel)
    3. Public Enum GMCars
    4.     <Description("Chevy SRV")> ChevySRV
    5.     <Description("Chevrolet aveo")> ChevroletAveo
    6.     <Description("Chevrolet Optra")> ChevroletOptra
    7.     <Description("Chevrolet Opel GT")> ChevroletOpelGT
    8.     <Description("Chevrolet Magnum Optra")> ChevroletMagnumOptra
    9.     <Description("Chevrolet Tavera")> ChevroletTavera
    10.     <Description("Chevrolet Tavera Neo")> ChevroletTaveraNeo
    11.     <Description("Chevrolet aveo U-VA")> ChevroletAveoUVA
    12.     <Description("Opel Astra")> OpelAstra
    13.     <Description("Opel Corsa")> OpelCorsa
    14.     <Description("Opel Corsa Swing")> OpelCorsaSwing
    15.     <Description("Chevrolet Captiva ")> ChevroletCaptiva
    16.     <Description("Chevrolet Malibu ")> ChevroletMalibu
    17. End Enum


    Now put this function in any module, so that it is accessible through the application:

    vb.net Code:
    1. ''This procedure gets the <Description> attribute of an enum constant, if any.
    2. ''Otherwise it gets the string name of the enum member.
    3. Public Shared Function EnumDescription(ByVal EnumConstant As [Enum]) As String
    4.     Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
    5.     Dim aattr() As DescriptionAttribute = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
    6.     If aattr.Length > 0 Then
    7.         Return aattr(0).Description
    8.     Else
    9.         Return EnumConstant.ToString()
    10.     End If
    11. End Function

    OK so now we are ready to use our setup with string values.
    Now each enum member contains a string value attached with it and we can get it using our EnumDescription function wherever we want the String value. Otherwise we would use it as we would use any other enum which represents numeric constants.

    Let’s fill it into a listbox for example:
    vb.net Code:
    1. ListBox1.Items.Add(EnumDescription(GMCars.ChevySRV))
    2. ListBox1.Items.Add(EnumDescription(GMCars.ChevroletOptra))
    3. ListBox1.Items.Add(EnumDescription(GMCars.ChevroletOpelGT))
    4. ListBox1.Items.Add(EnumDescription(GMCars.ChevroletAveoUVA))
    5. ListBox1.Items.Add(EnumDescription(GMCars.OpelAstra))

    Note that we no longer need to type the actual string values, but instead use the Enum members.

    The advantage you get with this method is the same as what you get with normal enums that represent numbers. You do not have to type the actual numeric value wherever you use it. Same way you do not need to type the String values in this case wherever you use this enum.

    In the above code sample I have used the procedure name "EnumDescription" to explain things here better, that makes the code lines look longer. But in actual practice I use very short notation (e.g. ED) so that it is hardly noticable. So the code line reduces to something like:
    vb.net Code:
    1. ListBox1.Items.Add(ED(GMCars.ChevySRV))

    Really easy. Isn't it?

    Pradeep
    Last edited by Pradeep1210; Apr 8th, 2009 at 10:31 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width