Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

VB
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
                    opt1 = 1 '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
                    opt1 = 0 'if this is checked i want to concat the string(opt) and integer(rdbtncnt) example opt & rdbtncnt = 0 ;but it gives me error
                ElseIf rdbtn.Text = "NA" Then
                    opt1 = 2 '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!
Posted
Updated 13-Aug-13 18:41pm
v2

1 solution

"Concatenation" is not defined on integer, isn't it? :-)

And don't concatenate repetitively even the string. This is inefficient, because strings are immutable. On each concatenation, the brand new instance of the string is created, old data is copied, and so on.

You need to use either string.Format or mutable System.Text.StringBuilder.

With Format, you can work with any types, for example:
C#
string result = string.Format("I'm using several integer values, this: {0}, this: {1}, this: {2} and that: {3}", opt, opt1, opt2, opt3);


In a cycle, you would need to use StringBuilder, but the data to be appended/inserted should be formatted to string using the universal method object.ToString().

[EDIT]

For example:
C#
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("some text");
sb.Append(someInteger.ToString());
sb.Append(someDateTime.ToString("yyyy/mm/dd hh:mm"));
sb.Append(anyObjectAtAll.ToString());
//...
string result = sb.ToString();


—SA
 
Share this answer
 
v3
Comments
Uknownymous 14-Aug-13 0:23am    
thank you for the reply sir. Then can you give me some sample code sir using "StringBuilder" or link, how to use it?

Last question sir. I have radiobuttons(namely radio1,radio2,radio3,...) then i use for loop then every loop the rdbtncnt is incrementing then the incremented value will be concat in control like if radio & rdbcnt &.checked = true then msgbox(radio.text)???
Sergey Alexandrovich Kryukov 14-Aug-13 0:34am    
I cannot imagine how could it cause any difficulties, but please see after [EDIT].
The main link is always from MSDN. Enter "System.Text.StringBuilder class" in Google (or any other class or method) and the most relevant MSDN page with code samples will be on top.

With next question, please feel free to post a separate one.

Well, are you accepting the answer formally now (green button)?
—SA
Uknownymous 14-Aug-13 0:42am    
check updated post sir.
Sergey Alexandrovich Kryukov 14-Aug-13 1:27am    
Great. But the way, this is good that you use parametrized SQL, otherwise SQL injection could be a problem.
—SA
Maciej Los 14-Aug-13 3:59am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900