C# 如何通过字符串转换为相就的参数

来源:百度知道 编辑:UC知道 时间:2024/06/06 01:17:36
private String x1 = 0;
private String x2 = 1;
private String x3 = 2;
private String x4 = 3;

public void print()
{
for (int i = 0; i < 4; i++)
{
String propertyName = "x"+(i+1);

Object property = "我如何通过 propertyName的值(也就是 x1 x2,x3 x4)这些字符串来 取得相已经定义的相应的变量呢?"

Console.WriteLine(property.ToString());

}
}

我想通过 propertyName的值(也就是 x1 x2,x3 x4)这些字符串来 取得相已经定义的相应的变量,该如何做 呢?

这个不行
可以通过其他方法
比如
string[] x =new string[]{"","","",""}
然后通过下标访问


拖4个Label进去设为隐藏(Visable=false)
然后按this.Controls["s"+i].Text访问

如果单就你想实现的功能来说建议你用haspmap做 用键值对应的方法

使用HASHTABLE就行了
string strinput="";
strinput = Console.ReadLine();
hashtable myhash = new hashtable();
myhash.add("x1",0);
myhash.add("x2",1);
myhash.add("x3",2);
myhash.add("x4",3);
Console.WriteLine((myhash[strinput]).ToString());

strinput 就是放置变量名的变量
输出的就是对应的数值

这个方法行不通 应该将值存在HashTable 或数组里面的