AshishS
(Fresh Scripter)
2011-03-02 12:03 AM
IF..ELSE...ENDIF Limitation?

How many times can you have ELSE statements in Kix? More specifically, is there a limit?
Instead of doing this....
IF @USERID = "GEN1"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER")
ELSEIF @USERID = "GEN2"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER 2")
ENDIF

Can I do this.....
IF @USERID = "GEN1"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER")
ELSEIF @USERID = "GEN2"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER 2")
ELSEIF @USERID = "GEN3"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER 3")
ELSEIF @USERID = "GEN 4"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER 4")
ENDIF


AllenAdministrator
(KiX Supporter)
2011-03-02 12:27 AM
Re: IF..ELSE IF...ENDIF Limitation?

I do not know of any limit. However you must have a ENDIF for every IF...

However in this case, it would appear that Select Case would be more appropriate.

 Code:
Select
  case @userid="Someone"
    ;do stuff
  case @userid="SomeoneElse"
    ;do stuff
;  case ...
Endselect


AshishS
(Fresh Scripter)
2011-03-02 12:53 AM
Re: IF..ELSE IF...ENDIF Limitation?

Allen,
Thanks for your feedback. Currently I'm using Case statement with a function to see if the computer belongs in a specific group and if it does, do some stuff. Would it be possible to modify my existing Case statement and include @USERID piece without breaking the script.
Below is part of my script...

Select
Case ComputerInGroup("Admin") = 0 and (@userid = "Gen1")
AddPrinterConnection("\\PRINTSERVER\HP PRINTER")
EndSelect

So can I modify my statement to this and would it still work properly?
Select
Case ComputerInGroup("Admin") = 0 and (@userid = "Gen1")
AddPrinterConnection("\\PRINTSERVER\HP PRINTER")
Case @userid = "Gen2"
AddPrinterConnection("\\PRINTSERVER\HP PRINTER2")
EndSelect


AllenAdministrator
(KiX Supporter)
2011-03-02 01:11 AM
Re: IF..ELSE IF...ENDIF Limitation?

Not exactly. Case is exclusive, meaning, once it finds one thing that is true, the rest of the cases are ignored. I doubt this is what you want. Case works well with @userid because everyone usually only has one. Case does not tend to work well with groups because users/computers can be in multiple groups.

You could always start a second select case grouping for @userid's, after the group select.


AshishS
(Fresh Scripter)
2011-03-02 01:17 AM
Re: IF..ELSE IF...ENDIF Limitation?

So based on what you said, does it mean that my existing case statement with ComputerInGroup & @userid is not working properly? Or are you saying that I can use Case with ComputerInGroup and @userid but not combine @userid in the next statement?

So this will work...
Select
Case ComputerInGroup("PATIENTROOM") = 0 and (@userid = "GEN1")

but not this...
Select
Case ComputerInGroup("PATIENTROOM") = 0 and (@userid = "GEN1")
do something
Case @userid
do something
EndSelect

Sorry if the questions sound kind of dumb. Just learning kix thats all.

Thanks for all your feedback as well. I really appreciate it.


AllenAdministrator
(KiX Supporter)
2011-03-02 01:34 AM
Re: IF..ELSE IF...ENDIF Limitation?

All of those will work... BUT... once one of those is true, the rest of the case are completely ignored.

I would use IF's for groups and SELECTs for userids

 Code:
if ingroup("groupname")
  ;do stuff
endif

if computeringroup("groupname")
  ;do stuff
endif

select
  case @userid="xxx"
    ;do stuff
  case @userid="yyy"
    ;do stuff
; ...
endselect


You might also consider reading up on using INI files, especially for the userids. You could create a single INI file that contains all the userid and the special drive mappings or printers that they need.

The ini would look something like
[user1]
printer=\\server\printer

[user2]
printer=\\server\printer2


There are several UDFs that read in the values for you. See ReadProfileString in the manual, or the link in my signature. You can also search the board, as there has to be hundreds of examples doing this.


AshishS
(Fresh Scripter)
2011-03-02 02:02 AM
Re: IF..ELSE IF...ENDIF Limitation?

Thanks for the feedback and pointing me in the right direction Allen.