What Does "& _" Mean in Vb?

What Does "& _" Mean in Vb?

I'm copying some query statements from a legacy VB app to a C# app. I am not familiar with VB, although looking at it makes me want a VB (Victoria Bitter). I have come across queries constructed like this:

*SELECT dp_duckbill_accounts.platypus_no AS duckbill, t_accounts.name AS Name " & _ 
"FROM t_accounts INNER JOIN dp_duckbill_accounts ON  t_accounts.account_no = dp_duckbill_accounts.account_no " & _
"ORDER BY dp_duckbill_accounts.platypus_no*

The "& _" give me pause. If it was just "&" I would think it corresponds to "+" in C# to concatenate strings. But what in the world is the point of the underscore? Note the ampersand and the underscore are separated by a space.

3

4 Answers

The underscore is the line continuation character. It allows the concatenation to include a different line. Like so:

x = "Hello " & "World"

x = "Hello " & _
    "World"

'this won't compile (pre vb.net 2010, anyway)
    x = "Hello " & 
    "World"

Line Continuation on MSDN

How to: Break and Combine Statements in Code (Visual Basic)

3

_ means continue the statement on the following line.

so ... & _ means continue concatenating the string on the following line.

text = "One line string"
text = "Two line " & _
       "string"

That is just a line continuation character that lets you continue to the next line.

& - is used for string concatenation in same line. example - sConcatenatedString = "First" & "Second"

& _ - is used For string concatenation in different lines. example - sConcatenatedString = "First" &_ "Second"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.