Click here to Skip to main content
15,907,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,


I have problem with enumeration in c# winforms application.

I have a Enum as foll0ws,

public enum Currency
{
   INR = 0,
   USD = 4545,
   JPY = 2152,
   EUR = 5241

}



I have populated the enum items in a combo box.


I want use the
combobox1.Text 
value to get the corresponding numeric value.

For Example :

When I select USD from the combobox, I have to Get the value of 4545 using the enum "Currency".




How can I achieve this...

Please help me.

Thanks in advance.
Posted

Use the Enum.Parse.

Enum.Parse (Type, String)
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Enum.Parse (Type, String, Boolean)
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-sensitive.
 
Share this answer
 
Comments
PeerMohamedMydeen 28-Jul-10 4:46am    
Thanks :)
Try:
enum testEnum
    {
    GBP = 100,
    USD = 200,
    }

...
testEnum te = (testEnum) Enum.Parse(typeof (testEnum), "USD");
...


[edit]
Oh, and in .NET 4.0 and above there is a (much preferable) TryParse:
Enum.TryParse<enumName>(stringNameOfEnumValue, out placeToPutValue);
[/edit]
 
Share this answer
 
v2
Comments
PeerMohamedMydeen 28-Jul-10 4:46am    
Thanks :)
This works fine for me (I've added a ComboBox called cbCurrencies on the form in design mode):
C#
public partial class Form1 : Form
{
    public enum Currency
    {
        INR = 0,
        USD = 4545,
        JPY = 2152,
        EUR = 5241
    }
    public Form1()
    {
        InitializeComponent();
        // Populate
        cbCurrencies.DataSource = System.Enum.GetValues(typeof(Currency));
    }
    private void cbCurrencies_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Get the selected currency
        Currency cur = (Currency)cbCurrencies.SelectedValue;

        // And get the value in need
        int value = (int)cur;

    }
}


:)
 
Share this answer
 
v2
Comments
PeerMohamedMydeen 28-Jul-10 4:47am    
Works Fine. Thanks :)
Nuri Ismail 28-Jul-10 4:53am    
You're welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900