#208715 - 2014-03-18 04:18 PM
IsDeclared
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
What am I doing wrong in the following script if this is not a bug?
Dim $RESULT
Dim $SOMEVAR
$RESULT = 0
$RESULT = SetOption("DisableDebugging", "On")
$RESULT = SetOption("Ascii", "Off")
$RESULT = SetOption("CaseSensitivity", "On")
$RESULT = SetOption("Explicit", "On")
$RESULT = SetOption("HideCursor", "Off")
$RESULT = SetOption("NoMacrosInStrings", "On")
$RESULT = SetOption("NoVarsInStrings", "On")
$RESULT = SetOption("WrapAtEOL", "On")
If @ONWOW64
$RESULT = SetOption("WOW64FileRedirection", "On")
$RESULT = SetOption("WOW64AlternateRegView", "On")
Else
$RESULT = SetOption("WOW64FileRedirection", "Off")
$RESULT = SetOption("WOW64AlternateRegView", "Off")
EndIf
$SOMEVAR = 5
Cls
? @CRLF
? TETESTEN($SOMEVAR)
Quit @ERROR
Function TETESTEN($ARG1)
If Not IsDeclared($RSLT)
Dim $RSLT
$RSLT = 0
? "RSLT=" + $RSLT + @CRLF
EndIf
$RSLT = 4
$TETESTEN = $RSLT * $ARG1
EndFunction
Output: RSLT=0
ERROR : undefined variable [RSLT]! Script: C:\tse_beta\source\Kix\putje.kix Line : 32
Line 32 is the one saying $RSLT = 4 Looks like outside the if endif the dim is not valid.
Edited by Allen (2014-03-18 04:26 PM) Edit Reason: added code tags
|
Top
|
|
|
|
#208717 - 2014-03-18 04:45 PM
Re: IsDeclared
[Re: Allen]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
thanx
|
Top
|
|
|
|
#208718 - 2014-03-18 05:08 PM
Re: IsDeclared
[Re: Henriques]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
I tried the global within the if endif, this works, however outside the function the var $RSLT can now also be used which was not my intention.
Dim $RESULT
Dim $SOMEVAR
$RESULT = 0
$RESULT = SetOption("DisableDebugging", "On")
$RESULT = SetOption("Ascii", "Off")
$RESULT = SetOption("CaseSensitivity", "On")
$RESULT = SetOption("Explicit", "On")
$RESULT = SetOption("HideCursor", "Off")
$RESULT = SetOption("NoMacrosInStrings", "On")
$RESULT = SetOption("NoVarsInStrings", "On")
$RESULT = SetOption("WrapAtEOL", "On")
If @ONWOW64
$RESULT = SetOption("WOW64FileRedirection", "On")
$RESULT = SetOption("WOW64AlternateRegView", "On")
Else
$RESULT = SetOption("WOW64FileRedirection", "Off")
$RESULT = SetOption("WOW64AlternateRegView", "Off")
EndIf
$SOMEVAR = 5
Cls
? TETESTEN($SOMEVAR) + @CRLF
? "RSLT=" + $RSLT + @CRLF
? @CRLF
Quit @ERROR
Function TETESTEN($ARG1)
If Not IsDeclared($RSLT)
Global $RSLT
$RSLT = 0
? "RSLT=" + $RSLT + @CRLF
EndIf
$RSLT = 4
$TETESTEN = $RSLT * $ARG1
EndFunction
Output: RSLT=0 20 RSLT=4
Edited by Mart (2014-03-19 09:42 AM) Edit Reason: Please use code tags when posting code.
|
Top
|
|
|
|
#208724 - 2014-03-18 07:54 PM
Re: IsDeclared
[Re: Glenn Barnas]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
I tried to see if a var already exists in the main or calling program. The function should be in an include. If the var was already globally declared in the calling program then a dim would give an error. To prevent this i was trying to only dim the var if it didn't exist yet.
|
Top
|
|
|
|
#208727 - 2014-03-18 09:10 PM
Re: IsDeclared
[Re: Glenn Barnas]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
No that's not true. In the Original script I had a dim of a var if not isdeclared. So, if there's no globally declaration in the calling program then and only then I make a locally. Well at least that's what I was trying to do.
|
Top
|
|
|
|
#208733 - 2014-03-19 02:43 PM
Re: IsDeclared
[Re: Lonkero]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
If a var already was globally declared would I get an error if I try to dim the var again.
|
Top
|
|
|
|
#208735 - 2014-03-19 04:06 PM
Re: IsDeclared
[Re: Henriques]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4562
Loc: USA
|
dim $var
$var="var"
? $var ;outputs var
test() ;outputs test
? $var ;outputs var
function test()
dim $var
$var="test"
? $var
endfunction
My apologies if this is not necessary, but I thought I would take a step back and explain the localization of variables. If we are only talking about dimming and not global, when you dim a variable inside a function(or other stucture), that var is only used inside the function. As you can see from above, the same variable name can be used multiple times without it effecting each other. It's probably not good practice to reuse variable names, but if you use setoption("explict", "on") and properly dim all your variables, there should be no reason why you can't re-use var names. I have many UDFs that use $i and $j for counters, and some of those UDFs call each other. However, because each function has the vars locally dimmed, there is no issue.
|
Top
|
|
|
|
#208738 - 2014-03-19 05:18 PM
Re: IsDeclared
[Re: Lonkero]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
Okay that means I can Dim within the function without checking (IsDeclared) so if it exist allready by a global declaration it will still create a var with the same name but internally using another memory space?
|
Top
|
|
|
|
#208742 - 2014-03-19 06:00 PM
Re: IsDeclared
[Re: Lonkero]
|
Henriques
Fresh Scripter
Registered: 2007-09-13
Posts: 43
|
Ok thanx
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1574 anonymous users online.
|
|
|