Page 1 of 1 1
Topic Options
#167076 - 2006-09-08 11:45 AM Check function arguments?
Daa Offline
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
#167077 - 2006-09-08 11:51 AM Re: Check function arguments?
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think you should not dim $ClientColor as it is in the arguments of your Function
Top
#167078 - 2006-09-08 01:07 PM Re: Check function arguments?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sounds odd still...
shouldn't happen.

Top
#167079 - 2006-09-08 02:23 PM Re: Check function arguments?
Witto Offline
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 Offline
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
#167081 - 2006-09-08 02:58 PM Re: Check function arguments?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
See IsDeclared() in the manual.
Top
#167082 - 2006-09-08 03:17 PM Re: Check function arguments?
Witto Offline
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
#167083 - 2006-09-08 03:20 PM Re: Check function arguments?
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
It is like giving this command
kix32.exe test.kix $KG=Whatever $CLIENT=

Top
#167084 - 2006-09-08 03:33 PM Re: Check function arguments?
Witto Offline
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
#167085 - 2006-09-08 04:52 PM Re: Check function arguments?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Sorry, my bad... guess it is declared so should do it in KiX with:

If $Client <> ""

Top
#167086 - 2006-09-08 05:00 PM Re: Check function arguments?
Witto Offline
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 Offline
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 Offline
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
#167089 - 2006-09-08 09:33 PM Re: Check function arguments?
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
OK, but if the second argument = 0, it will fail again!!!
I think the last update is the best. Turn the args in the select statement. 0 will be converted tot "0".

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 675 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.073 seconds in which 0.034 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org