【问题标题】:Concat string variable and integer variableConcat 字符串变量和整数变量
【发布时间】: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


【解决方案1】:

我想我理解你想要做什么 - 听起来你想根据你所在的 RadioButton 使用正确的 opt 变量 - 即,如果你在 RadioButton1,你想使用opt1,如果你在RadioButton2,你想使用opt2,等等。

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

有几种方法可以解决这个问题 - 您可以使用 List(Of Integer) 以与 RadiButtons 相同的顺序保存值,或者您可以使用 Dictionary(Of String, Integer) 保存名称作为键 (" opt" & RadionButton number) 和您选择的值。

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

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

注意事项

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

此外,您在外循环中增加了rdbtncntgrpboxcnt,我不确定这是否是您想要的(除非您知道每个GroupBox 中只有一个RadioButton)。

【讨论】:

【解决方案2】:

你可以试试:

rdbtncnt2Str = rdbtncnt.ToString()

rdbtncnt2Str = Convert.ToString(rdbtncnt)

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

将结果暗淡为字符串

结果 = opt & rdbtncnt

【讨论】:

    【解决方案3】:
     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"
    

    希望这会有所帮助..

    【讨论】:

    • 不..它没有工作..无论如何谢谢..我使用长方法。
    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 2020-10-23
    • 2018-12-30
    • 1970-01-01
    • 2013-12-30
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多