使用字符串中的值作为变量名称 [英] Use value in a string as variable name

查看:118
本文介绍了使用字符串中的值作为变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

字符串a =hallo

并且有一个包含a,b和c项的组合框。



假设所选项目为a



现在我的目的是使用一个(或b或c)作为变量名,

所以某些函数 GetVarFromString (a)返回hallo 。

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".

推荐答案

有一种方法可以使用字符串值作为变量名,但它的性能很重,我相信,如果你使用它,你就会遇到设计问题在你的代码中。所以在我向您展示使用字符串值作为变量的方法之前,我要求您考虑使用名称 - 值对来根据键获取值...



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



现在让我们看看吧。关键字是反射。反思 - 为此目的 - 只能用于班级的公共领域......

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...
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 将'Message 3'作为值...


szVal will get 'Message 3' as value...


自FrameWork 1.1起,ComboBox Control公开DisplayMember,和ValueMember,属性,您可以使用DataSource来处理您在此处描述的问题类型。 MS在这里有一个完整的例子:[ ^ ]。



您可以使用的另一种更简单的技术是创建表格的通用词典:
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:
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);
}

为什么我使用整数作为字典中的键,而不是字符串?因为ComboBoxItems集合根据定义是一个列表,它保证每个Item都有唯一的索引,而Dictionary必须有唯一的索引,并且因为使用所选Item的整数索引从Dictionary中获取字符串非常简单。 br />


上面提到的两种技术不是唯一的方法(除了Reflection)你可以根据ComboBox中的选择来处理交叉引用字符串的任务。 />


实验+观察+分析+研究==能力:)

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 :)


我不明白你的问题,如果你想要变量a要获得相同的变量c值,然后创建a = c;
I don't understand your problem, if you want variable a to have the same value of variable c then make a = c;


这篇关于使用字符串中的值作为变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆