vb.net Concat 字符串变量和整数变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18222858/
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
Concat string variable and integer variable
提问by Unknownymous
I have a form with 12 groupbox and each of that groupbox there is 3 radio buttons.
I use for loopto get what radio button is checked and get the value of radio button.
I declare string and integer variables to go through groupboxes and radiobuttons. My problem is how to concat string variable and integer variable. sample code :
我有一个包含 12 个分组框的表单,每个分组框都有 3 个单选按钮。我for loop用来获取选中的单选按钮并获取单选按钮的值。我声明字符串和整数变量以通过分组框和单选按钮。我的问题是如何连接字符串变量和整数变量。示例代码:
Dim opt, opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8, opt9, opt10, opt11, opt12 As String
Dim grpboxcnt As Integer = 1
Dim rdbtncnt As Integer = 1
Dim xrdbtn As String
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
If rdbtn.Checked = True Then
If rdbtn.Text = "Yes" Then
opt & rdbtncnt = 1 '(i want to be like this way but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 1 ;but it gives me error
ElseIf rdbtn.Text = "No" Then
opt & rdbtncnt = 0 '(i want to be like that but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked then i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 0 ;but it gives me error
ElseIf rdbtn.Text = "NA" Then
opt & rdbtncnt = 2 '(i want to be like that but it is an error and it is not the proper way, is there any way to concat string variable to integer variable?) if this is checked i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 2 ;but it gives me error
End If
End If
Next
rdbtncnt += 1 'then rdbtncnt increment new set of radiobuttons will be looped
grpboxcnt += 1 'then grpboxcnt increment new name of groupbox will be looped
Next
sqlcommand.commandType = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"
thanks in advance!
提前致谢!
回答by Tim
I think I understand what you're trying to do - it sounds like you want to use the correct optvariable based on which RadioButtonyou're in - i.e., if you're in RadioButton1, you want to use opt1, if you're in RadioButton2you want to use opt2, etc.
我想我明白你想要做什么 - 听起来你想根据你所在的位置使用正确的opt变量RadioButton- 即,如果你在RadioButton1,你想使用opt1,如果你在RadioButton2你想要使用opt2等
Unfortunately, you can't refer to a variable by concatenating two different variables to form it's name - at least, not that I'm aware of.
不幸的是,你不能通过连接两个不同的变量来引用一个变量来形成它的名字——至少,我不知道。
There's a couple of ways around this - you could use a List(Of Integer)to hold the value in the same order as the RadiButtons, or you could use a Dictionary(Of String, Integer)to hold the name as the key ("opt" & RadionButton number) and the value you select.
有几种方法可以解决这个问题 - 您可以使用 aList(Of Integer)以与RadiButtons相同的顺序保存值,或者您可以使用 aDictionary(Of String, Integer)将名称保存为键(“opt”和 RadionButton 编号)和您选择的值。
Since you appear to intend to use this to provide values for parameters to SQL command, I'd recommend the dictionary, since you can get the correct value by passing in the parameter name as a key:
由于您似乎打算使用它来为 SQL 命令提供参数值,我建议使用字典,因为您可以通过将参数名称作为键传递来获得正确的值:
Dim radioButtonValues As New Dictionary(Of String, Integer)
Dim grpboxcnt As Integer = 1
Dim rdbtncnt As Integer = 1
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
If rdbtn.Checked = True Then
If rdbtn.Text = "Yes" Then
radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 1)
ElseIf rdbtn.Text = "No" Then
radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 0)
ElseIf rdbtn.Text = "NA" Then
radioButtonValues.Add("@opt" & rdbtncnt.ToString(), 2)
End If
End If
Next
rdbtncnt += 1
grpboxcnt += 1
Next
This creates a Dictionary with "@opt1" as the key for the first RadioButtonList, "@opt2" for the second RadioButtonList, etc. I added "@" to the key because you'll use that as the parameter name in the following code:
这将创建一个辞典“@ OPT1”为重点的第一次RadioButtonList,“@ OPT2”第二RadioButtonList附加“@”的关键,因为你会使用如下面的代码中的参数名称等等我:
' Note that it should be CommandText, not CommandType
sqlcommand.CommandText = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"
sqlcommand.CommandType = CommandType.Text
For Each key As String in radioButtonValues.Keys
sqlcommand.Parameters.AddWithValue(key, radioButtonValues(key))
Next
Essentially, for each keyvalue pair in the dictionary, you add the key as the parameter name and it's value as the parameter value to the SQL command's parameter collection.
本质上,对于字典中的每个键值对,您将键作为参数名称添加,并将其值作为参数值添加到 SQL 命令的参数集合中。
NOTES
笔记
This is a little brittle, as you have to make sure you SQL Statement has the right number of parameters and they're named correctly. You could extend the code above to create the SQL command based on the keys in the dictionary and then add the parameters, but you'd wind up going through the dictionary twice, not once (once to build the SQL, a second time to build the parameters collection).
这有点脆弱,因为您必须确保 SQL 语句具有正确数量的参数并且它们被正确命名。您可以扩展上面的代码以根据字典中的键创建 SQL 命令,然后添加参数,但是您最终会遍历字典两次,而不是一次(一次构建 SQL,第二次构建参数集合)。
Also, you're incrementing rdbtncntand grpboxcntin the outer loop, and I'm not sure that's what you want (unless you know that there's only one RadioButtonin each GroupBox).
此外,你递增rdbtncnt,并grpboxcnt在外环,我不知道这是你想要的(除非你知道,只有一个RadioButton在每个GroupBox)。
回答by Abhishek
You can try:
你可以试试:
rdbtncnt2Str = rdbtncnt.ToString()
rdbtncnt2Str = rdbtncnt.ToString()
or
或者
rdbtncnt2Str = Convert.ToString(rdbtncnt)
rdbtncnt2Str = Convert.ToString(rdbtncnt)
This will convert the integer into string ,then you can concat using:
这会将整数转换为字符串,然后您可以使用以下命令进行连接:
Dim result As String
将结果调暗为字符串
result = opt & rdbtncnt
结果 = 选择 & rdbtncnt
回答by Abhishek
Dim opt, opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8, opt9, opt10, opt11, opt12 As String
Dim grpboxcnt As Integer = 1
Dim rdbtncnt As Integer = 1
Dim xrdbtn As String
Dim result As String
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For Each rdbtn As RadioButton In Me.Controls("GroupBox" & grpboxcnt).Controls.OfType(Of RadioButton)()
If rdbtn.Checked = True Then
If rdbtn.Text = "Yes" Then
opt1 = 1
ElseIf rdbtn.Text = "No" Then
opt1 = 0
ElseIf rdbtn.Text = "NA" Then
opt1 = 2
End If
rdbtncnt2Str = rdbtncnt.ToString()
result = opt1 & rdbtncnt2str
End If
Next
Integer.TryParse(result, rdbtncnt)
rdbtncnt += 1
grpboxcnt += 1
Next
sqlcommand.commandType = "UPDATE table1 SET radio1 = @opt1, radio2 = @opt2 , radio2 = @opt3 , etc... WHERE tableID = 1"
Hope this helps..
希望这可以帮助..

