1

Question:

The below program throws a runtime error (invalid conversion from double) because there is

 + _

followed by a newline and a

 +"

In other words, it's

"SomeString" + "someotherstring" ++ "yet another string"

 

<STAThread()> _
Sub Main()
    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder

    Dim dt As New DataTable
    dt.Columns.Add("oeId", GetType(String))
    dt.Columns.Add("ZO_RM_Name", GetType(String))
    dt.Columns.Add("ZO_GB_Name", GetType(String))
    dt.Columns.Add("gubeg", GetType(DateTime))
    dt.Columns.Add("guend", GetType(DateTime))

    Dim dr As DataRow = Nothing

    For i As Integer = 0 To 10 Step 1
        dr = dt.NewRow()
        dr("oeId") = "Organization Unit id " + (i * 1000).ToString()
        dr("ZO_RM_Name") = "Room " + i.ToString()
        dr("ZO_GB_Name") = "Building " + i.ToString()
        dr("gubeg") = System.DateTime.Now
        dr("guend") = System.DateTime.Now.AddDays(22).AddYears(20).AddHours(2)
        dt.Rows.Add(dr)
    Next

    For Each drThisRow As DataRow In dt.Rows
        sb.AppendLine("Organisationseinheit " + drThisRow("oeId").ToString() + _
                               +" in Raum " + drThisRow("ZO_RM_Name").ToString() + " von Gebäude " + drThisRow("ZO_GB_Name").ToString() _
                               + " in der Gültigkeit von " + drThisRow("gubeg").ToString() + " bis " + drThisRow("guend").ToString() + "." _
        )
    Next
End Sub '' Main ''

One could simplify this problem to this one:

 Dim newstring As String = "SomeString" + "someotherstring" + +"yet another string"

It compiles fine, but when the program is run, it throws a runtime error.
Is this a compiler bug ?
Shouldn't it stop me with a compiler error, like invalid syntax ?

11
  • 3
    Try enabling Option Strict. Commented Nov 9, 2011 at 11:01
  • @Lasse V. Karlsen: Yep, Option Strict on catches this, but it also throws a lot of other implicit conversion errors which aren't really any. Commented Nov 9, 2011 at 11:04
  • 4
    What you mean is: The compiler will complain about a lot of other conversions that you let slide until runtime, that just may actually not produce an error. Commented Nov 9, 2011 at 11:06
  • 1
    Due to operator precedence, the compiler may not actually bother figuring out if it could be a number because it might evaluate the string + dr[...] part first, and thus not be sure what the outcome will be. Still, now you know how to get rid of that warning, and new VB.NET programs should in all fairness be written with Option Strict On. Commented Nov 9, 2011 at 11:15
  • 2
    See msdn.microsoft.com/en-us/library/t0k7484c.aspx . I'd suggest strict=on, infer=off, explicit=on. It is more work, but worth it in the end. Also, use & for string concatenation. Commented Nov 9, 2011 at 13:03

2 Answers 2

4

The + operator comes in two variants:

  • a binary operator, a + b
  • a unary operator, +a

The first one says "take a and add b and produce the results". The second one says "take a with a positive sign".

In this case, the expression is evaluated as a string + a double, because VB.NET, with OPTION STRICT OFF will think that you want the second operand here as a string, that will be autoconverted to a double at runtime, because you prefixed it with the unary + operator.

Set OPTION STRICT ON to make VB.NET be more strict with its typechecking at compiletime, at the expense of you having to write code a bit more specific in some cases.

Sign up to request clarification or add additional context in comments.

Comments

2

Try using & to act as the concatenation operator in vb.net, this will cause a compile error without enabling Option Strict. I think what is happening is that due to there being no content between the operators it is using the second + as a sign for a number (e.g. +1). Due to VB default behaviour of implicitly converting it tries convert the string literal to a number.

Using Option Strict On will cause a compiler error to be displayed.

1 Comment

Be aware that & will implicitly add widening conversions to string, even with Option Strict On. These can be a nuisance, although they wouldn't make any difference for this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.