What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » if = true and also executing else

if = true and also executing else
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: if = true and also executing else
For starters remove the ; at the end of Wnd.Close(7) if you want to use the short form.

If you write IF THEN ELSE code in that way, thus seperated over multiple lines, remember that all those lines are actually always seen as 1 command line. But ; donates the end of a command line. So, it is a small syntax error, which can confuse the interpreter in some cases. It isn't realy wrong wrong in this case though since the interpreter is smart enough to interpret it properly in this particular case, but just be aware of it that it shouldn't be there.

But the real fault is in your Else syntax.
As you said so yourself, it should be IF THEN ELSE or IF THEN ELSEIF. But what you did is a IF THEN ELSEIF but without the IF.

Even in a language like Visual Basic (I assume you're used to that seeing that you said "if/then/else/elseif"), you need an IF codeword if you want to compare something else on the Else line:
code:
If ControlId = "BtnNo" Then
     CloseWindow Wnd, 7
ElseIF ControlId = "BtnYes" Then
     LockWorkStation "", "N"
End If

Thus what you actually wrote is (in Visual Basic terms):
vb code:
Function OnWndLockEvent_CtrlClicked(Wnd, ControlId)
     If ControlId = "BtnNo" Then
          CloseWindow Wnd, 7
     Else
          ControlId = "BtnYes"
     End If
     LockWorkStation "", "N"
End Function
Or  (in J(ava)Script Terms):
js code:
function OnWndLockEvent_CtrlClicked(Wnd, ControlId)
{
        if (ControlId == "BtnNo") {
                 Wnd.Close(7);
        } else {
                 ControlId == "BtnYes";
        }
        Interop.Call("User32.dll", "LockWorkStation", "", "N");
}

Hence your unexpected (but logical) behaviour.

Hence also my suggestion to always use brackets { } and not trying to shorten stuff, especially when debugging.
(this is a good example and exactly the reason why I did not agree with Eljay's comment here.)

Also, J(ava)Script doesn't have an ELSEIF statement. If you want to do an ELSEIF you actually must do another IF inside the first ELSE.

Thus either (in short form):
js code:
if(ControlId == "BtnNo")
     Wnd.Close(7)     
else if(ControlId == "BtnYes")
     Interop.Call("User32.dll", "LockWorkStation", "", "N");
Or either (in longer form):
js code:
if(ControlId == "BtnNo") {
     Wnd.Close(7)
} else if(ControlId == "BtnYes") {
     Interop.Call("User32.dll", "LockWorkStation", "", "N")
}
which is a shorter form of:
js code:
if(ControlId == "BtnNo") {
     Wnd.Close(7)
} else {
     if(ControlId == "BtnYes") {
          Interop.Call("User32.dll", "LockWorkStation", "", "N")
     }
}







quote:
Originally posted by dfnkt
Is that really considered good to to say:

If(blah)
   blah
If(blah2)
  blah

I've just always been accustomed to doing if/then/else/elseif type of a flow.
For stuff like this, where there are only 2 things to compare the ControlId to, it depends on what you're used to. All forms are considered good, as long as you don't make any errors in the syntax of course :p

Just remember that using multiple IF's is 'slower' than using the IF ELSE(IF) structure. This because in the first, all IF's will be executed and compared for a possible match, while in the later only the IF's and ELSE's up to a match will be executed.

Either way, if there are many more things to compare ControlId to, then you could consider using a switch structure (equivalent of the Select Case statement in Visual Basic).

This post was edited on 09-10-2010 at 11:35 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-10-2010 10:47 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
if = true and also executing else - by dfnkt on 09-10-2010 at 08:47 PM
RE: if = true and also executing else - by CookieRevised on 09-10-2010 at 10:47 PM
RE: if = true and also executing else - by dfnkt on 09-11-2010 at 04:04 AM
RE: if = true and also executing else - by Matti on 09-11-2010 at 09:19 AM
RE: RE: if = true and also executing else - by dfnkt on 09-11-2010 at 01:48 PM
RE: if = true and also executing else - by Eljay on 09-11-2010 at 02:04 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On