Satvir Singh
Satvir Singh

Reputation: 61

Long expressions spanning multiple lines

This is a simple question but it has been giving me a headache. I have a long if condition and instead of writing that on one line, I want to write on multiple lines for clarity.

I did some research and managed to find that using & _ , I can write the expression on the next line. However If I write something like:

If Not (DataGridView1.Rows(counter).Cells("User").Value Is Nothing) And & _
       (DataGridView1.Rows(counter).Cells("id number").Value Is Nothing) And & _
       (DataGridView1.Rows(counter).Cells("Emailaddress").Value Is Nothing) And & _
       (DataGridView1.Rows(counter).Cells("Date").Value Is Nothing) And & _
       (DataGridView1.Rows(counter).Cells("phone no.").Value Is Nothing) Then

....... 'do something here'
End If

The problem is putting And & _ the system expects a expression. I tried moving the & _ to other locations such as: before IS Nothing, but had no luck.

Can anyone please help me

Upvotes: 0

Views: 341

Answers (1)

Cody Gray
Cody Gray

Reputation: 244782

The And & part is definitely wrong, independent of the line breaks. You could put that whole mess on a single line (remove all of the line breaks) and it still wouldn't compile.

VB.NET uses And and AndAlso as the names of its bitwise and logical AND operators, respectively. And has bitwise semantics, like & in C#. AndAlso has logical and short-circuiting semantics, like && in C#. You should not (indeed, cannot) use both And and &. And you should be using AndAlso here, because you want logical, short-circuiting semantics.

The _ character at the end of a line serves as a line-continuation character. This is what you saw online, and this is correct. You can use this to break your expression up into multiple lines.

If Not (DataGridView1.Rows(counter).Cells("User").Value Is Nothing) AndAlso _
       (DataGridView1.Rows(counter).Cells("id number").Value Is Nothing) AndAlso _
       (DataGridView1.Rows(counter).Cells("Emailaddress").Value Is Nothing) AndAlso _
       (DataGridView1.Rows(counter).Cells("Date").Value Is Nothing) AndAlso _
       (DataGridView1.Rows(counter).Cells("phone no.").Value Is Nothing) Then

....... 'do something here'
End If

However, you might not need to do this at all. If you're using a relatively new version of VB.NET, implicit line continuation has been added to the language. Any operator (like And) will work as an implicit line-continuation operator. So you could just do:

If Not (DataGridView1.Rows(counter).Cells("User").Value Is Nothing) AndAlso
       (DataGridView1.Rows(counter).Cells("id number").Value Is Nothing) AndAlso
       (DataGridView1.Rows(counter).Cells("Emailaddress").Value Is Nothing) AndAlso
       (DataGridView1.Rows(counter).Cells("Date").Value Is Nothing) AndAlso
       (DataGridView1.Rows(counter).Cells("phone no.").Value Is Nothing) Then

....... 'do something here'
End If

Personally, I would compulsively line up the columns for readability purposes. But I think the VB.NET IDE will fight you on that, so it probably isn't worth it.

Upvotes: 3

Related Questions