#167076 - 2006-09-08 11:45 AM
Check function arguments?
|
Daa
Fresh Scripter
Registered: 2000-09-06
Posts: 14
Loc: Delft, Netherlands
|
What is wrong with the function below? If I call the function with an argument, all seems to work as expected. However, if I call the function without any arguments, the function returns 1. Debugging shows that it somehow finds a match on the first Case statement. Can anyone explain to me why? And how to prevent that from happening.
Code:
Function CheckClient ($ClientColor)
Dim $ClientColor, $FunctionResult $FunctionResult=0
Select
Case $ClientColor="Oranje" $FunctionResult=1
Case $ClientColor="Groen" $FunctionResult=ABS(1-(InGroup("\\" + @WKSTA + "\Administrators")))
EndSelect
$CheckClient=$FunctionResult
EndFunction
_________________________
Grtx, Dennis.
|
|
Top
|
|
|
|
#167079 - 2006-09-08 02:23 PM
Re: Check function arguments?
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
IMHO, the argument $ClientColor should not be (re)Dim(med) Put Setoption Explicit on top and it will complain Code:
$SO = SetOption("Explicit","On")
And I do not understand the error message as the $ClientColor parameter is not optional. I get: Code:
ERROR : invalid method/function call: missing required parameter 1! Script: D:\Scripts\test\test.kix Line : 10
|
|
Top
|
|
|
|
#167080 - 2006-09-08 02:47 PM
Re: Check function arguments?
|
Daa
Fresh Scripter
Registered: 2000-09-06
Posts: 14
Loc: Delft, Netherlands
|
Oops, my bad! I'm not calling the function without arguments, but with an argument that has nog been declared yet. Below some relevant code.
In short, here's the contents of a batchfile that *SHOULD* be called with 2 arguments.
Code:
@echo off kix32.exe script.kix $KG=%1 $CLIENT=%2
In script.kix I have the following code. Code:
If CheckClient($CLIENT)=1 Goto ClientOk Else Goto ErrorMessage EndIf
This works well if the batchfile is called with 2 arguments. These arguments are then passed to the Kix script as $KG and $CLIENT. However, if the batchfile is called without arguments, my function CheckClient still returns 1.
Hope to have clarified things a bit. I have removed the dim statement. Better code , but it still does not solve my problem.
|
|
Top
|
|
|
|
#167082 - 2006-09-08 03:17 PM
Re: Check function arguments?
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Les, I do not think so. I am testing it. I think the batch file should be tested for existence of %2 Code:
If NOT @LOGONMODE Break ON EndIf Dim $SO $SO = SetOption("Explicit","On") $SO = SetOption("NoMacrosInStrings","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("WrapAtEOL","On")
If IsDeclared($client) If CheckClient ($Client) = 1 ? "ClientOK" ? Else ? "ErrorMessage" ? EndIf EndIf
Function CheckClient ($ClientColor) Select Case $ClientColor="Oranje" $CheckClient=1 Case $ClientColor="Groen" $CheckClient=2;ABS(1-(InGroup("\\" + @WKSTA + "\Administrators"))) Case 1 $CheckClient=0 EndSelect EndFunction
Code:
@echo off REM echo parm0 = %0 REM echo parm1 = %1 REM echo parm2 = %2 REM echo parm3 = %3 REM echo parm4 = %4 REM echo parm5 = %5 REM echo parm6 = %6 REM echo parm7 = %7 REM echo parm8 = %8 REM echo parm9 = %9 If !%2 == ! GoTo end ..\..\kix32.exe test.kix $KG=%1 $CLIENT=%2 :end
|
|
Top
|
|
|
|
#167084 - 2006-09-08 03:33 PM
Re: Check function arguments?
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Strange... Is this a hidden feature... If I run Code:
kix32 test.kix $client=
it gives Code:
ClientOK
if I run Code:
kix32 test.kix $client
it gives Code:
ErrorMessage
|
|
Top
|
|
|
|
#167086 - 2006-09-08 05:00 PM
Re: Check function arguments?
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Correct me if I am wrong I think problem is here Code:
Case $ClientColor="Oranje"
$ClientColor = 0 (as an Integer) "Oranje" is being converted to an integer, Cint("Oranje"), = 0 0 = 0 return = 1 so what should be done is Code:
If IsDeclared($client) If CheckClient (CStr($Client)) = 1 ? "ClientOK" ? Else ? "ErrorMessage" ? EndIf EndIf
|
|
Top
|
|
|
|
#167087 - 2006-09-08 05:31 PM
Re: Check function arguments?
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Or another solution is switching the arguments in each case. In that way it is $ClientColor that wil be converted implicit. Code:
If NOT @LOGONMODE Break ON EndIf Dim $SO $SO = SetOption("Explicit","On") $SO = SetOption("NoMacrosInStrings","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("WrapAtEOL","On")
If IsDeclared($client) If CheckClient($Client) = 1 ? "ClientOK" ? Else ? "ErrorMessage" ? EndIf EndIf
Function CheckClient ($ClientColor) Select Case "Oranje" = $ClientColor $CheckClient=1 Case "Groen" = $ClientColor $CheckClient=2;ABS(1-(InGroup("\\" + @WKSTA + "\Administrators"))) Case 1 $CheckClient=0 EndSelect EndFunction
|
|
Top
|
|
|
|
#167088 - 2006-09-08 08:41 PM
Re: Check function arguments?
|
Daa
Fresh Scripter
Registered: 2000-09-06
Posts: 14
Loc: Delft, Netherlands
|
I now check the values of the arguments before calling the function. That has resolved my problem. Thanks for your help.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 675 anonymous users online.
|
|
|