If Statement

Each part of the if statement must be a single statement.
If we want to execute more than one statement then we must combine them into a single block statement using curly brackets.
In C# the parentheses around the condition to be tested is compulsory.


If

if (bValue == true) 
{
   sVariable1 = "text";
}

'VB.Net Equivalent
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) sVariable1 = "text"; 

'VB.Net Equivalent
If (bValue = True) Then sVariable1 = "text";
If (bValue = True) Then _
   sVariable1 = "text";

If - Else

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

'VB.Net Equivalent
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";

You can use the Tenary Operator

if (bValue == true) ? sVariable1 = "text" 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) 
{
}
else if (iValue < 20)
{
}
else
{
}

'VB.Net Equivalent
If (iValue < 10) Then

ElseIf (iValue < 20)

Else

End If

If (OR)

if ( ( a > b ) | ( c < d ) ) 
{
}

'VB.Net Equivalent
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 ) || ( c < d ) ) 
{
}

'VB.Net Equivalent
If ( a > b ) OrElse ( c < d ) Then
End If

If (AND)

if ( ( a > b ) & ( c < d ) ) 
{
}

'VB.Net Equivalent
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 ) && ( c < d ) ) 
{
}

'VB.Net Equivalent
If ( a > b ) AndElse ( c < d ) Then
End If

If Nested

if (bValue === false) 
{
   if (iValue < 10)
   {
   }
}

'VB.Net Equivalent
If (bValue = False) Then
   If (iValue < 10) Then
   End If
End If

Immediate IF

bool breturn; 
breturn = (inumber<10 ? svariable1="some" : svariable2="text")
breturn = (expression ? True action : False action)
Dim breturn As Boolean 
breturn = If(inumber<10, svariable1="some", svariable2="text")
breturn = If(expression, True action, False action)


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