VB.Net Syntax


If

If (bValue = True) Then 
   sVariable1 = "text";
End If

You can put everything on one line, but this makes it much harder to read.

If (bValue = True) Then sVariable1 = "text"; 
If (bValue = True) Then _
   sVariable1 = "text";

If - Else

If (bValue = True) Then 
   sVariable1 = "text";
Else
   sVariable2 = "text";
End If

You can leave out the curly brackets when there is only one statement to execute.
This makes it much harder to read and means you are limited to just one statement.

if (bValue == true) 
   sVariable1 = "text";
else
   sVariable2 = "text";

If - ElseIf - Else

Very important to remember that "else if" is 2 words in C# but "ElseIf" is 1 word in VB.Net.

If (iValue < 10) Then 

ElseIf (iValue < 20)

Else

End If

If (OR)

If ( a > b ) Or ( c < d ) Then 
End If

If (OR conditional)

You can use the "OrElse" if you do not want the second expression evaluated when the first expression is not true

If ( a > b ) OrElse ( c < d ) Then 
End If

If (AND)

If ( a > b ) And ( c < d ) Then 
End If

If (AND conditional)

You can use the "OrElse" if you do not want the second expression evaluated when the first expression is not true

If ( a > b ) AndElse ( c < d ) Then 
End If

If Nested

If (bValue = False) Then 
   If (iValue < 10) Then
   End If
End If

Immediate IF

Dim breturn As Boolean 
breturn = Microsoft.VisualBasic.Interaction.IIf(inumber<10, svariable1="some", svariable2="text")

IF Operator

This uses short-circuit evaluation to return one of two values.
This operator can be called with three arguments

If(argument1, argument2, argument3) 

The first argument must evaluate to a boolean.
When this operator has three arguments it works exactly the same as the Immediate IF
This operator can be called with two arguments.

If(argument1, argument2) 

The first argument is always returned unless it evaluates to Nothing, in which case the second argument is returned.


Select Case - End Case

Select Case Temp 
   Case "red"
      r += 1
   Case "green"
       r = r + 1
   Case "blue"
   Case Else : 'same line
End Select

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext