If you have an enumeration data that you need to list it out for selection, for example in ListBox control, you can use static methods provided in Enum class, e.g. GetNames and GetValues methods.
The static Enum.GetNames method will return an array of string of the enumeration value in textual format, as for Enum.GetValues will return an array of integer of the enumeration value.
For example, you have the enumeration below:
enum TrafficLight
{
Red = 1, Yellow = 2, Green = 4
}
You can populate the string value to a ListBox in Windows Application when the form is loading, like the code below:
... private System.Windows.Forms.ListBox lstTrafficLight; ... private void TestForm_Load(object sender, System.EventArgs e) { // some initialization here foreach (string strValue in Enum.GetNames(typeof(TrafficLight))) { lstTrafficLight.Items.Add(strValue); } // other action }
This will populate Red, Yellow, and Green into the ListBox control. However, if you use Enum.GetValues method, it will populate 1, 2 and 4 instead.
Popularity: 2% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply