【发布时间】:2013-08-14 03:46:06
【问题描述】:
我有一个包含 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"
提前致谢!
【问题讨论】:
-
您是否尝试在运行时创建变量名?即,如果 opt1 设置为 1,在您要设置的第一个组中创建一个名为
opt11的变量,其值为 1?您不能在运行时创建变量名。有几个问题可以帮助您获得答案 - 您当前遇到的错误是什么,您最终要如何处理您尝试创建的变量? -
查看更新后的帖子(评论部分)
-
@ReignOfRayn25 - 简短的回答是你不能做你想做的事(至少据我所知);但是,请查看我的答案以了解可能对您有用的替代方法。
标签: vb.net winforms visual-studio-2012 for-loop concatenation