Concat字符串变量和整数变量 [英] Concat string variable and integer variable
问题描述
我有一个带有12个groupbox的表单,每个groupbox有3个单选按钮。我使用for循环来获取选中的单选按钮并获取单选按钮的值。我声明字符串和整数变量来通过groupboxes和radiobuttons。我的问题是如何连接字符串变量和整数变量。示例代码:
Dim opt,opt1,opt2,opt3, opt4,opt5,opt6,opt7,opt8,opt9,opt10,opt11,opt12 作为 字符串
Dim grpboxcnt 作为 整数 = 1
Dim rdbtncnt As 整数 = 1
Dim xrdbtn < span class =code-keyword> As String
For 每个 grpbx 作为 GroupBox 在 < span class =code-keyword> Me .Controls.OfType( of GroupBo x)()
对于 每个 rdbtn 作为 RadioButton 在 Me .Controls( GroupBox& grpboxcnt).Controls.OfType( RadioButton)()
如果 rdbtn.Checked = < span class =code-keyword> True 然后
如果 rdbtn。 Text = 是 然后
opt1 = 1 ' 如果选中此项我想要连接字符串(opt)和整数(rdbtncnt)示例opt& rdbtncnt = 1;但它给了我错误
ElseIf rdbtn.Text = 否 然后
opt1 = 0 ' 如果选中此项我想连接字符串(opt)和整数(rdbtncnt)示例opt& rdbtncnt = 0;但它给我错误
ElseIf rdbtn.Text = NA 然后
opt1 = 2 ' 如果选中此项我想连接字符串(opt)和整数(rdbtncnt)示例opt& rdbtncnt = 2;但它给我错误
结束 如果
结束 如果
下一步
rdbtncnt + = 1 ' 然后rdbtncnt增量新的radiobutton组将被循环
grpboxcnt + = 1 ' 然后grpboxcnt增加groupbox的新名称将循环
下一步
sqlcommand .commandType = UPDATE table1 SET radio1 = @ opt1,radio2 = @ opt2,radio2 = @ opt3等... WHERE tableID = 1
提前致谢!
Concatenation没有定义整数,不是吗? :-)
即使是字符串,也不要重复连接。这是低效的,因为字符串是不可变的。在每个连接上,创建字符串的全新实例,复制旧数据,等等。
您需要使用string.Format
或者mutableSystem.Text.StringBuilder
。
随着格式
,您可以使用任何类型,例如:
string result = string .Format( I我使用了几个整数值,这个:{0},这个:{1},这个:{2}和那个:{3},opt,opt1,opt2,opt3);
在一个循环中,你需要使用StringBuilder
,但要附加/插入的数据应使用通用方法格式化为字符串object.ToString()
。
例如:System.Text.StringBuilder sb = new System.Text。串生成器();
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
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
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!
"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 eitherstring.Format
or mutableSystem.Text.StringBuilder
.
WithFormat
, you can work with any types, for example:
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 useStringBuilder
, but the data to be appended/inserted should be formatted to string using the universal methodobject.ToString()
.
[EDIT]
For example: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
这篇关于Concat字符串变量和整数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!