English 中文(简体)
Concat strings by & + in VB. 净额
原标题:Concat strings by & and + in VB.Net

Is there any difference between & and + operators while concatenating string? if yes, then what is difference? And if No, then why below code generating exception?

例:

    Dim s, s1, t As String
    Dim i As Integer

    s1 = "Hello"
    i = 1

    s = s1 & i
    t = s1 + i  //Exception here

    If s = t Then
        MessageBox.Show("Equal...")
    End If
最佳回答

以及 + 是二类操作者,但当你在使用+时指出一种分类时,即设法将“Hello”投入分类,添加。 如果你将“Hello”改为“123”,你将获得结果124。

问题回答
  • & is only used for string concatenation.
  • + is overloaded to do both string concatenation and arithmetic addition.

<代码>+<>>/代码”的双重目的造成了混乱,与你的问题完全一样。 特别是当<代码>Option Strict>时,因为汇编者将在你的笔迹和笔录中添加含蓄的文字,试图使你的代码具有意义。

我的建议

  • You should definitely turn Option Strict On, then the compiler will force you to add explicit casts where it thinks they are necessary.
  • You should avoid using + for concatenation because of the ambiguity with arithmetic addition.

这些建议也载于Microsoft Press book 。 BB和C#实用准则和最佳做法(第1.16、21.2节)

你也许会转而选择Strict(这是一件好事),汇编者告诉你,你可以做些补充。 为此:

<代码>t = s1 & i.ToString(

如你的问题所证实,它们是不同的:& is ONLY string concatenation,+,上载的既有正常的添加,也有分类。

例如:

  • 由于<代码>+的歌剧之一,是VB企图将脚ing改成愤怒,而你的扼杀不是数字;

  • 。 仅与胎体合作,使 in化转化为扼杀。

您可以撰写和复制;增加插播和编造:

processDetails=objProcess.ProcessId & ":" & objProcess.name
message = msgbox(processDetails,16,"Details")

产出如下:

5577:wscript.exe

Try this. It almost seemed to simple to be right. Simply convert the Integer to a string. Then you can use the method below or concatenate.

Dim I, J, K, L As Integer
Dim K1, L1 As String

K1 = K
L1 = L
Cells(2, 1) = K1 & " - uploaded"
Cells(3, 1) = L1 & " - expanded"

MsgBox "records uploaded " & K & " records expanded " & L

从以前的扼杀组合(分机)来看,你应当真正考虑使用“强令”。 格式而不是分类。

    Dim s1 As String
    Dim i As Integer
    s1 = "Hello"
    i = 1
    String.Format("{0} {1}", s1, i)

它使事情更容易阅读和维护,我认为你的法典更加专业。 见:code better - use string。 格式。 虽然并非每个人都同意。 最好使用强硬。 格式与说明分类?

我的两点:

如果你策划了大量的扼杀,那么你就应当使用“StingBuilder”。 海事组织更清洁,大大faster





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签