borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Regarding Registry and Visual Basic)Next Topic (IMHO:Dates and MS-SQL/Access) New Topic New Poll Post Reply
AndreaVB Forum : Frequently Asked Questions : if...if...else/if elseif.../if...ifnot
Poster Message
sad777
Level: Professor

Registered: 01-07-2003
Posts: 81

icon if...if...else/if elseif.../if...ifnot

hiya peeps!

i got a question in connection with IF

i have the following code ie:

if lblOne = 1 then
   dothings
    if lblOne <> 1 then  'not equal?
     otherthings
else
donethings
end if


or

if lblOne = 1 then
   dothings
    if not lblOne = 1 then  'not equal?
     otherthings
else
donethings
end if


or

if lblOne = 1 then
   dothings
    elseif lblOne <> 1 then  'not equal?
     otherthings
else
donethings
end if


which one of these codes will work and will the most efficient? and is this my 'not equal? < string correct?

as i need to use this code to move homedirs and ids in containers... so please help

    

thanks again   

____________________________
-- Deus Caritas est --
Virtutis Gloria Merces

25-01-2004 at 10:53 AM
View Profile Send Email to User Show All Posts | Quote Reply
vivekjain
Level: Professor

Registered: 11-08-2003
Posts: 71
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon 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
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
sad777
Level: Professor

Registered: 01-07-2003
Posts: 81
icon Re: if...if...else/if elseif.../if...ifnot

hmmm...i c

so basically the code would look like this?

    Select Case lblOne.caption
        
        Case 0

                 lblOne.caption = "1"
                 dothings

        Case 1

                 lblOne.caption = "2"
                 otherthings

        Case 2

                 lblOne.caption = "0"
                 dosomethingelse

end select


ive only worked with the "Case Select" in a dropdownbox form. so i hope that this is how its suppose to be?

thanks for the info       

[Edited by sad777 on 25-01-2004 at 10:04 PM GMT]

____________________________
-- Deus Caritas est --
Virtutis Gloria Merces

25-01-2004 at 08:01 PM
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: if...if...else/if elseif.../if...ifnot

Yup! That's pretty much it. I was about to correct something but saw you edit it. Basically that is how to use Select Case. Happy coding.

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

25-01-2004 at 08:29 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
sad777
Level: Professor

Registered: 01-07-2003
Posts: 81
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: if...if...else/if elseif.../if...ifnot

I'll tell you what your code is doing then you can determine if that is what you want.

1
If the caption of lblOne is "1" then you are doing things to 1 "dothingsto1"

2
If the caption of lblOne is not "1" then dothingsto2 provided the caption of lblTwo is = "2". In other words, if the caption of lblTwo <> (is not equal to) "2" then nothingisdoneto2 (this bit of code is not executed)

3
If lblOne is not "1" neither is lblTwo equal to "2" then this bit of code is executed. So if lblTwo is equal to "1" or "3" or any other number, nothingisdone. The same if lblOne is anything but "1", nothingisdone

I hope you understood that and it shed some light on what you want to know. If not, don't hesitate to post back. Happy coding.

[Edited by fabulous on 25-01-2004 at 11:13 PM GMT]

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

25-01-2004 at 09:08 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
sad777
Level: Professor

Registered: 01-07-2003
Posts: 81
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
fabulous
Level: VB Guru


Registered: 03-08-2002
Posts: 439
icon Re: if...if...else/if elseif.../if...ifnot

Goran is right. I was going to address that

If you want to do default stuff whenever lbl22 is "22" regardless of the outcome of any other test you will have to do it this way.

If lbl22 = "22" Then
  DodefaultStuff
End If

If lblOne = "server\user" Then
  'do what you want
  MoveToDifferentServer
Else
  'Handle exceptions
End If


Nice to help a work mate (same boss). I hope this helps. Happy coding.

____________________________
My boss is a Jewish Carpenter (Jesus Christ)


Brain Bench Certified VB.NET Developer

26-01-2004 at 01:39 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
sad777
Level: Professor

Registered: 01-07-2003
Posts: 81
icon Re: if...if...else/if elseif.../if...ifnot

thanks again,

it worked, if implemented both case and elseif, in different places obvioulsy.

but thanks again

____________________________
-- Deus Caritas est --
Virtutis Gloria Merces

28-01-2004 at 09:49 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 933
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1052
icon Re: if...if...else/if elseif.../if...ifnot

Guys,

check out my post on select case true.. ive never seen it before, but it is very useful in multi-line if's
Kieron

____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

24-09-2005 at 02:48 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Frequently Asked Questions : if...if...else/if elseif.../if...ifnot
Previous Topic (Regarding Registry and Visual Basic)Next Topic (IMHO:Dates and MS-SQL/Access) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2009 Andrea Tincaniborder