VB6 always executes the 2nd function even though the 1st is False

Asked

Viewed 196 times

4

The if in VB6 always executes the 2nd function even validating that the 1st is False, C# does not execute the second function in case the first one is already false.

How can I get the vb6 to perform the 2nd function, only if the 1st is true?

Code c#:

if(CalculoHora() && ValidaDados())
{}

Code VB6:

if (CalculoHora and ValidaDados) then

end if
  • 1

    I’m not sure if VB6 has it (VB.NET has it), but it experiences the Andalso condition instead of the And

1 answer

7


This evaluation of logical expressions is known as "short-circuit evaluation" (Short-Circuit Evaluation): Short-Circuit Evaluation

Unlike C# for example, in VB this validation is not performed, that is, in a sequence of logical operators, all are validated until determining whether the expression is true or false, for example:

If a > b And b > a And b <> a Then

In VB.Net, there are operators AndAlso and OrElse who do this.

Here is an answer from the OS in English on this subject: https://stackoverflow.com/questions/4014918/does-vb6-short-circuit-complex-conditions

The answers confirm that there is no resource at VB6, and the suggestions are to validate in smaller blocks with Then If and Select Case

Read more (in English) here: Short-Circuit Evaluation in Visual Basic.Net

  • 1

    Interestingly, I thought all languages implemented this!

  • 1

    Yeah, the VB6 is an exception in many things

Browser other questions tagged

You are not signed in. Login or sign up in order to post.