 |
vivekjain Level: Professor
 Registered: 11-08-2003 Posts: 71
|
Re: if...if...else/if elseif.../if...ifnot
Hi,
This code seems to be fine
if lblOne = 1 then
dothings
elseif lblOne <> 1 then 'not equal?
otherthings
else
donethings
end if
<> signifies 'not equal to'
|
|
25-01-2004 at 11:06 AM |
|
|
fabulous Level: VB Guru

 Registered: 03-08-2002 Posts: 439
|
Re: if...if...else/if elseif.../if...ifnot
Hi,
A piece of advice. If you are going to test the same variable for different situations I would suggest that you use Select Case. The reason is that If...ElseIf tests each time the ElseIf statement is hit.
Try running this code to see what I mean. (Click No).
Private Sub Form_Load()
If MsgBox("Click a button", vbYesNoCancel, "Click") = vbYes Then
MsgBox "Yes"
ElseIf MsgBox("Click a button", vbYesNoCancel, "Click") = vbNo Then
MsgBox "No"
ElseIf MsgBox("Click a button", vbYesNoCancel, "Click") = vbCancel Then
MsgBox "Cancel"
Else
MsgBox "How did this happen?", vbQuestion
End If
End Sub |
Why it does this is because the If statement (of comparison structure) does not memorise the result, so it will test each time. Try hitting this path No, Cancel, Yes, you will end up in the else block which should never be run. Your user will be confused why (s)he is being asked the question many times.
If is like a child who has to study before tackling each qustion in his homework while Select Case studies once and tackles the questions with background knowledge.
The If...ElseIf structure should be used when testing different conditions/variables, for instance if someone has to be a programmer to get credit or otherwise has to earn > $,1000,000 each month ElseIf would fit in just right. Consider this code.
'supposing the value for occupation and salary were obtained earlier
If Lcase(strOccupation) = "programmer" Then
MsgBox "You're the man"
ElseIf dblSalary > 1000000 Then
MsgBox "Well, we could work something out."
Else
MsgBox "Go away!"
End If |
This is the ideal place for ElseIf, testing different variables for one or two conditions.
Here is what I normally suggest and what I teach to those under me (when it comes to testing)
One variable one condition - If
One variable two conditions - If..Else
One variable > two conditions - Select Case
> One variable - If....ElseIF....ElseIf....Else
If you follow that I am convinced you will test only when necessary and will have maximum effeciency.
Hope that helps. Happy coding.
[Edited by fabulous on 25-01-2004 at 06:01 PM GMT]
____________________________
My boss is a Jewish Carpenter (Jesus Christ)

Brain Bench Certified VB.NET Developer
|
|
25-01-2004 at 03:58 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: if...if...else/if elseif.../if...ifnot
And this code has one 'else' that is sufficiant.
if lblOne = 1 then
dothings
elseif lblOne <> 1 then 'not equal?
otherthings
else
donethings
end if |
If you put it this way, ELSE statement will never be executed. Why, because you test for lblOne value is equal to 1 or not. So you have to cases to handle. So, u only need this:
if lblOne = 1 then
dothings
else
dootherthings
end if |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
25-01-2004 at 04:38 PM |
|
|
sad777 Level: Professor
 Registered: 01-07-2003 Posts: 81
|
Re: if...if...else/if elseif.../if...ifnot
hiya
just another question, in implementing it...
ok, i have 2 var
ie:
lblOne and lblTwo
if lblOne = "1" then
dothingsto1
elseif lblTwo = "2" then
dothings2
else
nothingisdone
end if |
is that a correct assumption? in the usage of elseif?
____________________________
-- Deus Caritas est --
Virtutis Gloria Merces
|
|
25-01-2004 at 08:59 PM |
|
|
sad777 Level: Professor
 Registered: 01-07-2003 Posts: 81
|
Re: if...if...else/if elseif.../if...ifnot
hiya,
ok i understand the idea much more, thanks very much 
ok here is what i used it for
and this is what ive done,
if lblOne = "server\user" then
domovetodifferentserver
elseif lblTwo = lbl22 then
dodefaultstuff '< this is one the default update proc. lbl22 i get from the details that i import, so if lbltwo = lbl22 then it just updates, which is the default proc.
else
domove
end if |
atm im importing a file of +- 3500 users, and it seems that, that specific part of the code is working without any issues.
thanks again, and i c we work for the same boss ;)
the only problem im still having is with the novell AX controls, but they looking into it, at least, thanks again to all 
____________________________
-- Deus Caritas est --
Virtutis Gloria Merces
|
|
26-01-2004 at 08:17 AM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: if...if...else/if elseif.../if...ifnot
sad777, considering this code, :
if lblOne = "1" then
dothingsto1
elseif lblTwo = "2" then
dothings2
else
nothingisdone
end if |
This has some effects that you might not need. For instance, if lblOne=1, no matter what value is lblTwo dothings2 will never be executed. I dont know if that is wthat you wanted, but I wanteed to direct you on possible problem.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
26-01-2004 at 12:08 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: if...if...else/if elseif.../if...ifnot
Again about...
Many programmers often forget the single-row format of the If...Then statement, even if code result sometimes a bit harder readable.
' single row
If x > 0 Then y = x
' single row with Else clause
If x > 0 Then y = x Else y = 0
' single row with more instructions separated by double commas
If x > 0 Then y = x: x = 0 Else y = 0
' multiline, same as above
If x > 0 Then
y = x
x = 0
Else
y = 0
End If
' without "short-circuit" of And condition
If x = 0 And Sqr(y) < z Then z = 0
' does the same, but with a nested If, to short-circuit the And condition (faster)
If x > 0 Then If Sqr(y) < z Then z = 0
' multiline, same as above
If x = 0 Then
If Sqr(y) < z Then
z = 0
End If
End If
' without "short-circuit" of Or condition
If x = 0 Or x * y > 100 Then z = 0
' does the same, but with an ElseIf clause, to short-circuit the Or condition (faster)
If x > 0 Then z = 0 ElseIf x * y > 100 Then z = 0
' multiline, same as above
If x > 0 Then
z = 0
ElseIf x * y > 100 Then
z = 0
End If
' longer instruction
Dim a As Boolean, b As Boolean
a = False
If a = InputBox("Insert b value", "Test titlebar, but very very long") _
Then b = a: MsgBox "b (first) = " & b & "; a = " & a Else MsgBox "a = " _
& a & ";b (second) = " & b
|
Note the End If is only needed in multilines blocks.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
30-01-2004 at 11:29 AM |
|
|
|
|
 |
 |