0

In VB .Net it is possible to use inline If statements, like this

if a Then ret = "A" Else ret = "Not A"

I know it is also possible to nest these statements. I know this might not be good practice as readability drops down...

If a Then If b Then ret = "A & B" Else ret = "A & Not B" Else ret = "Not A"

which will be evaluated like this :

If a Then
    If b Then
        ret = "A & B"
    Else
        ret = "A & Not B"
    End If
Else
    ret = "Not A"
End If

Now if I remove the last Else statement, I get this :

If a Then If b Then ret = "A & B" Else ret = "A & Not B"

which is :

If a Then
    If b Then
        ret = "A & B"
    Else
        ret = "A & Not B"
    End If
End If

So I guess the compiler finds the Else and matches it to the last openend If in the line, which makes sense. My question is : Is it possible to change the evaluation order ? By that I mean is it possible that the Else is matched with the first if ?

So it would look like this :

If a Then
    If b Then
        ret = "A & B"
    End If
Else
    ret = "Not A"
End If

But inlined (something like this, parenthesis added to understand what I mean) :

If a Then ( If b Then ret = "A & B" ) Else ret = "Not A"

I tried with parenthesis or adding a End If but it provokes syntax errors, and I couldn't find documentation for this.

I'm not stuck anywhere and I know this might not be a good prorgamming practice, but I just wanted to know (out of curiosity) if it was possible.

I know also I could switch the statements (i.e. test b before a), but let's say I can't (a is a List and must be tested for Nothing before b which would be the Count for example).

1
  • If you are confusing the compiler, imagine yourself trying to make sense of that 6 months down the road Commented Nov 10, 2016 at 15:04

3 Answers 3

6

You could do it like this

ret = If(a, If(b, "A & B", "A & Not B"), "Not A")

Personally, I never understood the need to put everything in one line.

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

7 Comments

Problem with this is that both statements get evaluated (From here).
That link says the opposite @MartinVerjans. Short circuiting means only the part that applies is evaluated. the inner If will only execute when a is true otherwise it "jumps" to the false part
The If operator is different than the If statement though @MartinVerjans
@MartinVerjans IIF behave differently than IF.
Is there any imports to do or reference to add ? Using VS 2012 Express and .Net 3.5, the compiler doesn't recognize it...
|
2

You may find that this is what you need:

If a And B Then

   ret = "A & B"

Else

   ret = "Not A"

End If

Or as a single line:

If A And B Then Ret = "A & B" Else RET = "Not A"

Comments

0

I just created a simple console application with your final nested if statement and it worked fine

Sub Main()
    Dim a = False
    Dim b = True
    If a Then
        If b Then
            Console.WriteLine("A & B")
        End If
    Else
        Console.WriteLine("A & Not B")
    End If
    Console.ReadLine()
End Sub

1 Comment

I know this would work, I am asking if it's possible to do that in an inline statement.

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.