Concat字符串变量和整数变量

时间:2013-08-14 03:46:07

标签: vb.net winforms visual-studio-2012 for-loop concatenation

我有一个带有12个groupbox的表单,每个groupbox有3个单选按钮。 我使用for loop来获取选中的单选按钮并获取单选按钮的值。 我声明字符串和整数变量来通过groupboxes和radiobuttons。我的问题是如何连接字符串变量和整数变量。示例代码:

 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"
提前谢谢!

3 个答案:

答案 0 :(得分:2)

我想我明白你要做什么 - 听起来你想根据你所在的opt使用正确的RadioButton变量 - 也就是说,如果你在RadioButton1,如果您想opt1使用RadioButton2等,则希望使用opt2

不幸的是,你不能通过连接两个不同的变量来形成它的名称来引用变量 - 至少,不是我所知道的。

有几种解决方法 - 您可以使用List(Of Integer)按照与RadiButton相同的顺序保存值,或者您可以使用Dictionary(Of String, Integer)来保存将名称作为键(“opt”& RadionButton编号)和您选择的值。

由于您似乎打算使用它来为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

这会创建一个词典,其中“@ 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

基本上,对于字典中的每个keyvalue对,您可以将键作为参数名称添加,并将其值作为SQL命令参数集合的参数值。

备注

这有点脆弱,因为你必须确保SQL语句具有正确数量的参数并且它们被正确命名。您可以扩展上面的代码,根据字典中的键创建SQL命令,然后添加参数,但是最终会通过字典两次,而不是一次(一次构建SQL,第二次构建参数集合。)

另外,你在外循环中递增rdbtncntgrpboxcnt,我不确定你想要的是什么(除非你知道每个中只有一个RadioButton GroupBox)。

答案 1 :(得分:0)

您可以尝试:

rdbtncnt2Str = rdbtncnt.ToString()

rdbtncnt2Str = Convert.ToString(rdbtncnt)

这会将整数转换为字符串,然后您可以使用:

连接

昏暗的结果为字符串

结果= opt& rdbtncnt

答案 2 :(得分:0)

 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"

希望这会有所帮助..