Page 1 of 1 1
Topic Options
#153119 - 2005-12-08 06:32 PM Just for the record
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
Just for the record.
Do not DIM a passed parameter variable.
God I'm an idiot sometimes.
_________________________
Taxation WITH representation isn't so hot, either!

Top
#153120 - 2005-12-08 06:39 PM Re: Just for the record
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
lol, you know how many times I've done that ? Too many to count. One thing I can say - it makes for a very difficult bug to track down.
Top
#153121 - 2005-12-08 08:13 PM Re: Just for the record
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Hmmm I thought the new KiX versions would tell you right away what the issue is.

Using these values works well for me.

Code:
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')


 

Top
#153122 - 2005-12-08 08:26 PM Re: Just for the record
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
If I understand this correctly.

I pass a parameter to a Kix script like this

Code:
 
kix32.exe somescript.kix $somevariable="somestring"



Now if I DIM the variable in the script

Code:
 
DIM $somevariable



The action of DIMing the variable makes its value NULL(Is that right?)

Now help me out here

If I pass a parameter like I did above, does the script consider it dimmed?

If not how do I DIM it without losing its contents?
_________________________
Taxation WITH representation isn't so hot, either!

Top
#153123 - 2005-12-08 08:32 PM Re: Just for the record
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Quote:


The action of DIMing the variable makes its value NULL(Is that right?)





Thats right.

Quote:


If I pass a parameter like I did above, does the script consider it dimmed?





Yes and no. It considers it as GLOBAL, as if you had done this:

Global $somevariable

Quote:


If not how do I DIM it without losing its contents?





As far as I know you cant. Just assume command line $vars are Global.

Top
#153124 - 2005-12-08 08:35 PM Re: Just for the record
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
Thanks Shawn.
That cleared it up for me.
_________________________
Taxation WITH representation isn't so hot, either!

Top
#153125 - 2005-12-08 08:36 PM Re: Just for the record
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
now we come to the very idea of the explicit option.
set explicit on and try then find out was the var set on commandline or not...
_________________________
!

download KiXnet

Top
#153126 - 2005-12-08 08:39 PM Re: Just for the record
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Not to hammer this home any futher, but the following clearly demonstrates this behavior:

Code:

Break On

$= SetOption("Explicit", "On")

?"value=" $somevariable

Exit 0



We first set Explicit ON, which means this:

Quote:


When you enable the Explicit option, you must explicitly declare all variables using Dim, Global or ReDim statements. If you attempt to use an undeclared variable name, an error occurs. Use the Explicit option to avoid incorrectly typing the name of an existing variable.





Then if you run the script like this:

C:\> kix32 test.kix

You get this:

ERROR : undefined variable [somevariable]!
Script: E:\t.KIX
Line : 7

But then if you run the same script like this:

C:\> kix32 test.kix $somevariable=1

You get no syntax error. $somevariable passes the test as being declared (Global in this case).

-Shawn

Top
#153127 - 2005-12-08 08:41 PM Re: Just for the record
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
I did have explicit on which was why I thought I would have to DIM it, but alls well that ends well.

Once a NOOB always a NOOB.
_________________________
Taxation WITH representation isn't so hot, either!

Top
#153128 - 2005-12-08 11:23 PM Re: Just for the record
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Stanley... you know I thought you were talking about passing variables to a UDF rather than a script. I'm glad you posted this as I'm sure this would have bitten me at some point too.

At one point when I first started using Explicit/DIM, I thought I had to dim the variables in the UDF header within the function, ie...
Code:

function($var1,$var2)
dim $var1,$var2
;...
endfunction


Interestingly it works, but I believe it was Jooel, who smacked me around a bit and told me it only worked because Kix was so forgiving. So try not to make that mistake.

Top
#153129 - 2005-12-09 04:21 AM Re: Just for the record
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
I have some tools that get args passed on the command line. However, it's possible that the tool could be called directly instead of by the bat file, and the var won't be declared. Here's one way I handle that situation, where the lack of a value prints an error message, or possibly takes a default value:
Code:
If Not IsDeclared($CL_ARG)		; is usually passed, but must be declared if not passed
Global $CL_ARG
; can define default value here
EndIf



Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#153130 - 2005-12-09 09:24 AM Re: Just for the record
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmmm...
glenn, doesn't your code actually force kixtart to end processing if not declared?
you can't test $CL_ARG as explicit says you haven't declared it and bombs out.
_________________________
!

download KiXnet

Top
#153131 - 2005-12-12 10:27 AM Re: Just for the record
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

hmmm...
glenn, doesn't your code actually force kixtart to end processing if not declared?
you can't test $CL_ARG as explicit says you haven't declared it and bombs out.




Did you try it? I guess not

I use the same trick.

IsDeclared() has special processing which means that the script does not abort if the variable is undeclared. Just as importantly, checking if the variable exists does not create it implicitly.

Top
Page 1 of 1 1


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

Who's Online
1 registered (Allen) and 271 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.064 seconds in which 0.022 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