Tuesday 27 March 2012

Beginning .net : How to Enumarate an Enum ?

You can able to enumerate an enum to get it's values and names.
Example :
We have an emum Called "Sports".
   Public Enum Sports
        Cricket
        Hockey
        Soccer
        BaseBall
        Tennis
    End Enum

You can enumerate this by Values and by Names.

Following code demostrate how you can enumerate by Values.
Example :
   For Each strValue As String In [Enum].GetValues(GetType(Sports))
            Response.Write(strValue.ToString())
            Response.Write("</br>")
   Next

Following code demostrate how you can enumerate by Names.
Example :
  For Each strName As Object In [Enum].GetNames(GetType(Sports))
            Response.Write(strName.ToString())
            Response.Write("</br>")
  Next

No comments:

Post a Comment