7 Replies - 3507 Views - Last Post: 23 April 2012 - 02:41 PM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Inline IF condition is True - VB6 also looks at False part?

Posted 22 April 2012 - 03:18 AM

General
Dim rsBrand As New ADODB.Recordset


Private Sub Form_Load()
rsBrand.Open "tblBrand", Cnn, , adLockOptimistic
End Sub

Private Sub cboBrand_DropDown()
Call PopulateCombo(cboBrand, rsBrand, 1)
End Sub

Public Sub PopulateCombo(ByVal cbo As ComboBox, ByVal rs As ADODB.Recordset, ByVal No As Integer)
With rs
    .Requery
    Do While Not .EOF
        cbo.AddItem IIf(cbo.Name = "cboBrand", .Fields(No), .Fields(1) & " " & .Fields(2))
        .MoveNext
    Loop
End With


There is the error message "Run-time error '3265': Item cannot be found in the collection corresponding to the requested name or ordinal." and the line cbo.AddItem IIf(cbo.Name = "cboBrand", .Fields(No), .Fields(1) & " " & .Fields(2)) is highlighted.

Placing the cursor on .Fields(2) shows the error message. The error is because tblBrand has only 2 fields. However, since cbo.Name = "cboBrand", does VB6 not go only to the True part (.Fields(No))? It looks at the False part (.Fields(1) & " " & .Fields(2)) also (and thus causes the error)?

I prefer the Inline IF function to the If...Then...Else statement for IIF takes only 1 line. I will have to use If...Then...Else if IIF does not work in this case. What about the advantages and disadvantages of each over each other?

This post has been edited by AN1554: 22 April 2012 - 03:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Inline IF condition is True - VB6 also looks at False part?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 22 April 2012 - 11:20 AM

IIF evaluates both possible result arguments, before make the choice. This maybe ok for your code as long both side don't have side effects.
Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 11:00 AM

Yes, it is so, just as VB evaluates all conditions in an AND clause even if the first one isn't met (another sometimes-annoying and closely-related feature of VB). If you need to avoid evaluating all conditions if some of them aren't met, then as you surmise you have to formally lay everything out in if else blocks.

For example:
If myObject <> Nothing And myObject.myProperty = 19 Then
    'Do whatever
End If

If myObject <> Nothing Then
    If myObject.myProperty = 19 Then
        'Do whatever
    End If
End If

On the face of it, the first if block with the And operator looks like the same thing as the second if block expressed in more concise and elegant fashion. It isn't! The first one will error out, because it will go on to evaluate myObject.myProperty whether the object = Nothing or not. VB will always evaluate both sides of an And operation, even if the first condition is not satisfied and therefore the And can't be true. Conversely, VB will also always evaluate both sides of an Or operation, even if the first condition is satisfied and therefore the Or must be true. You have to be careful not to figure that the second condition will only evaluate to an error if the first condition is or isn't met, because you will get an error anyway.

Vb.Net has an AndAlso and an OrElse to allow "short-circuiting" of logical operators, where the second condition will not be evaluated if the first condition satisfies the requirements of the operator. This is really just syntax sugar, since nested ifs will do the same job as you can see here.
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 11:16 AM

A couple of other observations on your code, AN: Don't use "As New" in your code, as it's very inefficient. This doesn't actually instantiate the code and save you a line as it would seem. What it does is checks each time you reference your object to see if it has been instantiated yet, and if not, instantiates it. So every time you reference rsBrand at all, it goes through all this unnecessary overhead.

Next, unlike primitives, passing object references (cbo and rs in your code) ByVal is exactly the same as passing them ByRef. To convince yourself if you like, perform this experiment. (Put a label and two command buttons on a form.)
Private Sub Command1_Click()
Dim x as Integer
x = 73
Label1.Caption = "Pressed Command1"
TryByVal x, Label1
Form.Print "x=" & x
End Sub

Private Sub Command2_Click()
Dim x as Integer
x = 47
Label1.Caption = "Pressed Command2"
TryByVal x, Label1
Form.Print "x=" & x
End Sub

Private Sub TryByVal(ByVal myInteger As Integer, ByVal myLabel As Label)
myInteger = 472
Label1.Caption = "Called TryByVal"
End Sub

Private Sub TryByRef(myInteger As Integer, myLabel as Label)
myInteger = 628
Label1.Caption = "Called TryByRef"
End Sub

You should find (if I've written everything correctly) that only TryByRef changes the x variable, but both TryByRef and TryByVal change the Label's caption.

Why do you think that might be? :)

Finally, "while not" and "until" are the same in VB, just in case you didn't know that. I use until personally to always avoid the not operator, but the most important thing is to be consistent.

This post has been edited by BobRodes: 23 April 2012 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 12:02 PM

Just to point out that vb.net has a short-circuiting inline-if
Dim result=If(x<10,"A","B")


if you use the following it has vb6 semantics to be compatible.
Dim result=IIf(x<10,"A","B")


This post has been edited by AdamSpeight2008: 23 April 2012 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 01:47 PM

Oo, didn't know that. Good to know Adam.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 01:57 PM

View PostBobRodes, on 23 April 2012 - 07:16 PM, said:

Finally, "while not" and "until" are the same in VB, just in case you didn't know that. I use until personally to always avoid the not operator, but the most important thing is to be consistent.


There not the same

While Not somecondition
...
WEnd 

Do 
...
Until Not someCondition


In a Do..Until loop the code in the loop will executed at least once, in a While Loop the code in the loop may never be executed if the overall evaluation of the condition is false.
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Inline IF condition is True - VB6 also looks at False part?

Posted 23 April 2012 - 02:41 PM

View PostAdamSpeight2008, on 23 April 2012 - 09:57 PM, said:

View PostBobRodes, on 23 April 2012 - 07:16 PM, said:

Finally, "while not" and "until" are the same in VB, just in case you didn't know that. I use until personally to always avoid the not operator, but the most important thing is to be consistent.


There not the same

While Not somecondition
...
WEnd 

Do 
...
Loop Until Not someCondition

Actually, yes they are so the same. :) Your code sets forth a straw man, for while it is indeed true that setting the condition at the end of the loop requires the loop to execute at least once, while setting it at the beginning doesn't, that has nothing to do with whether while and until are the opposite of each other. Your second code example (which you will pardon me for having corrected before referencing--you left out the "Loop" keyword) could just as well say "Loop While someCondition" and it would impose exactly the same condition.

Your first code example is the equivalent of
Do While Not someCondition
...
Loop

The difference is that While/Wend was deprecated as of VB4, for the simple reason that it didn't allow postconditions on the loop as you basically show. The current constructs are these:
'precondition loops
Do Until '= Do While Not
Loop

Do While '= Do Until Not
Loop

'postcondition loops
Do
Loop Until '=Loop While Not

Do
Loop While '= Loop Until Not



VB.Net has the same thing. There is still a While/End While, but there is also the equivalent of the four Do loops here.

This post has been edited by BobRodes: 23 April 2012 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1