Need confirmation about ElseIf statement 
Author Message
 Need confirmation about ElseIf statement

Hi,

I just want to confirm that if I type in "1" at the InputBox below,
then the section marked by comments will NOT get tested, right?

It's just that I've heard it's best to use "If-ElseIf-EndIf" when
comparing differing results instead of a lot of "If/Then" tests,
and as you can see using "Select Case" won't work in this situation.

Dim temp As String
temp = InputBox("Enter a number:")
If Left$(temp, 1)="1" Then
    MsgBox "Yes, temp = 1"
' Does this next section get tested if temp = 1?
' Or is it bypassed because the test was satisfied above?
ElseIf Left$(temp, 2)="22" Then
    MsgBox "Yes, temp = 22"
' End section
End If

Regards, Otser.



Wed, 18 Jun 1902 08:00:00 GMT  
 Need confirmation about ElseIf statement

Quote:
> I just want to confirm that if I type in "1" at the InputBox below,
> then the section marked by comments will NOT get tested, right?
[...]
> If Left$(temp, 1)="1" Then
>     MsgBox "Yes, temp = 1"
> ' Does this next section get tested if temp = 1?
> ' Or is it bypassed because the test was satisfied above?
> ElseIf Left$(temp, 2)="22" Then
>     MsgBox "Yes, temp = 22"
> ' End section
> End If

The expression following ElseIf will not be evaluated unless temp
starts with something other than "1".  To prove this, use the following
test code:

    Dim objColl As Collection
    Dim strTest As String

    strTest = "1"

    If Left$(strTest, 1) = "1" Then
    ElseIf objColl.Count > 1 Then
    End If

When you run the above code, nothing happens because strTest = "1".
Now change the code so that strTest = "2", and you will get an error 91
on the ElseIf expression, because objColl was never set.  This proves
that the ElseIf expression was not executed when strTest = "1".

HTH-Jon

Sent via Deja.com http://www.deja.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Need confirmation about ElseIf statement

Quote:
> The expression following ElseIf will not be evaluated unless temp
> starts with something other than "1".

That's what I thought -- thanks!  :)

Regards, Otser.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. nested if/elseif statements

2. If Then ElseIf Statements

3. Elseif Statements

4. IF ELSEIF statements in VBScript?

5. Modal forms - need confirmation

6. Need to read sql2k table thru ado, need connection statement

7. if then elseif problem

8. Select case/if then elseif

9. Select Case Vs If - ElseIf

10. If Then ElseIf the solution to this bug?

11. Elseif Condition in Crystal Reports 4.6

12. If-Then-Elseif

 

 
Powered by phpBB® Forum Software