vb.net 使用字符串作为变量名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15356108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using string as a name of a variable
提问by conquistador
Is it possible to use a string as a name of a variable? For Example..
I declared x as a private double
是否可以使用字符串作为变量的名称?例如..
我将 x 声明为私有双精度
Private TextBox1Store,TextBox2Store,TextBox3Store As Double
I will use that as a variables for storing value.
我将使用它作为存储值的变量。
This function multiplies the number inside a label and textbox and returns a product.
此函数将标签和文本框中的数字相乘并返回一个产品。
Private Function mqtyCalc(ByVal y As Integer, ByVal z As Integer) As Integer
Dim w As Integer
w = y * z
Return w
End Function
This part handles three textbox events.
这部分处理三个文本框事件。
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
Dim tb As TextBox = sender
Dim tempObjNm As String
tempObjNm = tb.Name + "Strore"
tempObjNm = mqtyCalc(someVariable.Text, Label1.Text)
End Sub
And this the part I'm trying to solve.
这就是我试图解决的部分。
tempObjNm = someVariable.Name + "Strore"
tempObjNm = mqtyCalc(tb.Text, Label1.Text)
The "tempObjNm" is declared inside this sub as string.
Everytime I Input a value inside the textboxs, this sub will get the name of the textbox that has been inserted a value and the name will be added "Store" at their end Jus to mimic the variable name declared above. Example,
“tempObjNm”在此子程序中声明为字符串。
每次我在文本框内输入一个值时,这个 sub 将获取已插入值的文本框的名称,并且该名称将在其末尾添加“Store”以模仿上面声明的变量名称。例子,
temObjNm = TextBox1Store (mimicking the Private TextBox1Store)
temObjNm is currently a string declared by
temObjNm = TextBox1Store(模仿 Private TextBox1Store)
temObjNm 当前是一个由以下声明的字符串
Dim tempObjNm As String
As a string
作为字符串
This part is the storing part of the sub
这部分是sub的存放部分
tempObjNm = mqtyCalc(tb.Text, 4)
(Take note that the value of tempObjNm = "TextBox1Store"
(注意 tempObjNm = "TextBox1Store" 的值
When I print TextBox1Store, it prints 0
当我打印 TextBox1Store 时,它打印 0
How is that? Is it not possible to use a string for mimicking the varible just to store value in it?
那个怎么样?是否不能使用字符串来模拟变量只是为了在其中存储值?
采纳答案by SysDragon
Just do this:
只需这样做:
Dim tb As TextBox = CType(sender, TextBox)
Me.Controls(tb.Name & "Store") = mqtyCalc(CInt(someVariable.Text), CInt(Label1.Text))
I strongly suggest you a couple of things. First, enable Option Strict Onin you project properties, as it will improve your programming practices. And, as you can see in my code, concatenate strings with &instead of +in VB.NET.
我强烈建议你做几件事。首先,Option Strict On在您的项目属性中启用,因为它将改进您的编程实践。而且,正如您在我的代码中看到的&,+在 VB.NET 中使用而不是连接字符串。
回答by djv
Can you use a Dictionary(Of String, Double)?
你能用Dictionary(Of String, Double)吗?
Private values As New Dictionary(Of String, Double)
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
setValue(sender)
End Sub
Private Sub setValue(sender As Object)
Dim tb As TextBox = CType(sender, TextBox)
Dim tbName As String = tb.Name & "Strore"
If Not values.ContainsKey(tbName) Then
values.Add(tbName, tb.Text)
Else
values(tbName) = tb.Text
End If
End Sub
Private Function getValue(sender As Object) As Double
Dim tbName As String = CType(sender, TextBox).Name & "Strore"
If Not values.ContainsKey(tbName) Then
Return Double.NaN
Else
Return values(tbName)
End If
End Function
回答by Camahalan Royette
I would like to share the code that I am using. Hope this helps future viewers of this question.
我想分享我正在使用的代码。希望这有助于这个问题的未来观众。
Dim tb As TextBox = sender
Dim tempObjNm As String
tempObjNm = tb.Name + "Strore"
Me.GetType.GetField(tempObjNm).SetValue(Me, CType(mqtyCalc(someVariable.Text, Label1.Text), Double))

