C#将字符串解释为现有数组变量 [英] C# interpret string as an existing array variable

查看:187
本文介绍了C#将字符串解释为现有数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





非常简单的问题。

我需要将字符串名称(从控件名称中提取)解释为现有的数组变量名。



这是我简化的代码,以便很好地理解



  //  我的数组定义 
string [] _dev = { DC 192.168.1.1 魁北克 独立};

// 字符串
string str1 = Server_dev;

//提取名称
string str2 = str1.Substring(str1.Length - 4)//结果现在是字符串
_dev < span class =code-string>

//现在我需要显示数组_dev的元素'2':

textBox1.Text = str2.Trim( )[2] //但是我的_dev数组名称没有被解释为请求...





有什么想法?< br $> b $ b

非常感谢



Ben

解决方案

< blockquote>你没有使用内省,所以这个简化的声明将会这样做:



变量的名称不能被另一个变量的值选择。



本声明:



 textBox1.Text = str2.Trim()[ 2  ]; 





...给你2号角色 - 在这种情况下 - 是字母'e'。



如果你期待魁北克,你可以这样做:



 textBox1.Text = _dev [ 2 ]; 


Hi,

Very simple question.
I need to interpret a string name (extracted from a control name) as an existing array variable name.

Here is the code I simplify to well understand

// My array definition
string[] _dev = { "DC", "192.168.1.1", "Quebec", "Standalone"};

// The string
string str1 = "Server_dev;

// The extraction of the name
string str2 = str1.Substring(str1.Length - 4)     // the result is now the string "_dev"

// Now I need to display the elements '2' of the array _dev :

textBox1.Text = str2.Trim()[2]        // But my _dev array name is not interpreted as requested...



Any idea ?

Thanks a lot

Ben

解决方案

You aren't using introspection so this simplified statement will do:

A variable's name is not select-able by another variable's value.

This statement:

textBox1.Text = str2.Trim()[2];



... gives you the character at position 2 which - in this case - is the letter 'e'.

If you were expecting "Quebec", you could do this:

textBox1.Text = _dev[2];


这篇关于C#将字符串解释为现有数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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