Reputation: 993
I have a form with 12 groupbox and each of that groupbox there is 3 radio buttons.
I use for loop
to 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 :
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!
Upvotes: 0
Views: 8388
Reputation: 28530
I think I understand what you're trying to do - it sounds like you want to use the correct opt
variable based on which RadioButton
you're in - i.e., if you're in RadioButton1
, you want to use opt1
, if you're in RadioButton2
you want to use opt2
, etc.
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 RadiButton
s, or you could use a Dictionary(Of String, Integer)
to hold the name as the key ("opt" & RadionButton number) and the value you select.
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:
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:
' 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.
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).
Also, you're incrementing rdbtncnt
and grpboxcnt
in the outer loop, and I'm not sure that's what you want (unless you know that there's only one RadioButton
in each GroupBox
).
Upvotes: 2
Reputation: 882
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..
Upvotes: 0
Reputation: 882
You can try:
rdbtncnt2Str = rdbtncnt.ToString()
or
rdbtncnt2Str = Convert.ToString(rdbtncnt)
This will convert the integer into string ,then you can concat using:
Dim result As String
result = opt & rdbtncnt
Upvotes: 0