Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Let say I have
string a = "hallo"
and have a combo-box with items "a", "b" and "c".

Let say that the selected item is "a"

Now my purpose is to use the value "a" (or "b" or "c") as variable name,
so some function GetVarFromString("a") returns "hallo".
Posted
Updated 12-Nov-13 9:27am
v3
Comments
Maciej Los 12-Nov-13 16:58pm    
Not enough information!
Please, be more specific and provide more details, for example about data binding.

There is a way to use string value as variable name, but it's a performance heavy, and I believe, that if yo use it you have a design problem in your code. So just before I show you the way to use string value as variable I ask you to consider using name-value pairs to get value according to key...

http://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx[^]

Now let see it. The keyword is reflection. And reflection - for this purpose - can be used only on public fields of a class...
C#
public class ErrorMessages
{
    public string Msg1 = "Message 1";
    public string Msg2 = "Message 2";
    public string Msg3 = "Message 3";
}

ErrorMessages oErrorMessages = new ErrorMessages ( );

string szVal = Convert.ToString ( typeof ( ErrorMessages ).GetField ( "Msg3" ).GetValue ( oErrorMessages ) );

szVal will get 'Message 3' as value...
 
Share this answer
 
Comments
CHill60 12-Nov-13 16:09pm    
My 5. I was heading down the reflection route but struggling...missed the trick of having the messages in a class (d'oh). Nice solution. Oh - and I agree with the name-value pairings as the better way forward :-)
Since FrameWork 1.1, the ComboBox Control exposes DisplayMember, and ValueMember, Properties which you can use with a DataSource to handle exactly the type of problem you describe here. MS has a complete example here:[^].

Another, simpler, technique you can use is to create a generic Dictionary of the form:
C#
private Dictionary<int,> dctComboXRef = new Dictionary<int,>
{
    {0, "hallo"},
    {1, "goodbye"},
    {2, "whatever"}
};

private void YourForm_Load(object sender, EventArgs e)
{
    foreach (var kvp in dctComboXRef)
    {
        comboBox1.Items.Add(kvp.Key);
    }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedAsString = dctComboXRef[comboBox1.SelectedIndex];

    Console.WriteLine("selected " + selectedAsString);
}
Why did I use integers as the key in the Dictionary, rather than a string ? Because a ComboBoxItems collection is, by definition, a list which guarantees every Item has a unique index, and a Dictionary must have unique indexes, and because it's so simple to fetch the string from the Dictionary using the integer index of the Item selected.

The two techniques mentioned above are not the only ways (besides Reflection) you could handle the task of cross-referencing a string based on the selection in a ComboBox.

experiment + observe + analyze + study == competence :)
 
Share this answer
 
I don't understand your problem, if you want variable a to have the same value of variable c then make a = c;
 
Share this answer
 
Comments
Member 10321326 12-Nov-13 15:30pm    
This was a example not in my solution.

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