Page 1 of 1 1
Topic Options
#208715 - 2014-03-18 04:18 PM IsDeclared
Henriques Offline
Fresh Scripter

Registered: 2007-09-13
Posts: 43
What am I doing wrong in the following script if this is not a bug?
 Code:
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
#208716 - 2014-03-18 04:39 PM Re: IsDeclared [Re: Henriques]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
A DIM inside a if/endif structure is only used inside that structure. You will likely need to use GLOBAL instead of DIM.
Top
#208717 - 2014-03-18 04:45 PM Re: IsDeclared [Re: Allen]
Henriques Offline
Fresh Scripter

Registered: 2007-09-13
Posts: 43
thanx
Top
#208718 - 2014-03-18 05:08 PM Re: IsDeclared [Re: Henriques]
Henriques Offline
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.

 Code:
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
#208719 - 2014-03-18 05:18 PM Re: IsDeclared [Re: Henriques]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Maybe you should describe what you are trying to do. If you are concerned the var is defined outside of the function, creating a global would be no different, right?
Top
#208720 - 2014-03-18 06:40 PM Re: IsDeclared [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
You can be fairly certain that variables declared in the main script as Local (Dim) will not be available in a function. Globals will be..

"If IsDeclared(var)" is usually used in the Main program to declare a var. A common use is when you sometimes pass $VarX on the command line.. it is auto-declared as a Global in that manner. If you don't pass the var but test it, you'll get an Undeclared varname error. Adding the
 Code:
If NotIsDeclared($VarX)Global $VarX EndIf
logic will allow arbitrary passing of that var on the command line without worrying about parsing errors.

It's also commonly used in an initialization function to define global vars only during the first call to the Init function to insure that you don't duplicately define the variables.

Your first example doesn't fit a common method.. Your second example follows the Init methodology.

As Allen suggests - tell us more about what you want to do.

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

Top
#208721 - 2014-03-18 06:54 PM Re: IsDeclared [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
what he seems to want is persistence of the variable between calls to the UDF.
and global variable is pretty much only way of doing it.
_________________________
!

download KiXnet

Top
#208722 - 2014-03-18 07:10 PM Re: IsDeclared [Re: Lonkero]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Yes, aside from passing a value to and returning it from the UDF...
_________________________
Actually I am a Rocket Scientist! \:D

Top
#208724 - 2014-03-18 07:54 PM Re: IsDeclared [Re: Glenn Barnas]
Henriques Offline
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
#208725 - 2014-03-18 08:53 PM Re: IsDeclared [Re: Henriques]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Yes, but expecting a global to possibly be present and then declaring a local could have strange results elsewhere in the script.

It's probably best to use
 Code:
If Not Isdeclared(Var) Global Var EndIf


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

Top
#208727 - 2014-03-18 09:10 PM Re: IsDeclared [Re: Glenn Barnas]
Henriques Offline
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
#208732 - 2014-03-19 02:49 AM Re: IsDeclared [Re: Henriques]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you can dim how ever much you want. what you mean it gives error if there is already an variable?
that would be a bug.
_________________________
!

download KiXnet

Top
#208733 - 2014-03-19 02:43 PM Re: IsDeclared [Re: Lonkero]
Henriques Offline
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 Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
 Code:
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
#208736 - 2014-03-19 04:41 PM Re: IsDeclared [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yea. specially for UDF's, you do not need to care about the global namespace, other UDFs or anything else.
it is a script itself with it's own name space. as long as you do not declare anything global, you are in your own little world inside UDF.
_________________________
!

download KiXnet

Top
#208738 - 2014-03-19 05:18 PM Re: IsDeclared [Re: Lonkero]
Henriques Offline
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
#208741 - 2014-03-19 05:52 PM Re: IsDeclared [Re: Henriques]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yup.

and as you saw in your if isdeclared() headache, even an if block has it's own name space.
same goes for loops and such. but at this point those might confuse more than help.

so, the short answer is yes.


Edited by Lonkero (2014-03-19 05:54 PM)
_________________________
!

download KiXnet

Top
#208742 - 2014-03-19 06:00 PM Re: IsDeclared [Re: Lonkero]
Henriques Offline
Fresh Scripter

Registered: 2007-09-13
Posts: 43
Ok thanx
Top
#208743 - 2014-03-19 07:07 PM Re: IsDeclared [Re: Henriques]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
A variation on Allen's example:
 Code:
Global $X
$X=3.1514
Func($X)

Function Func($_V)

  'X=' $X ?	; X is a global, value displays
  'V=' $_V ?

  Dim $X	; define a local var called X

  'X=' $X ?	; X is now local, Dim'd but unset - nothing to see here!
  'V=' $_V ?
  
  Exit 0

EndFunction
Note that once you declare X locally, the value in the function is empty. Try replacing the DIM with GLOBAL. \:\)

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

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
0 registered and 1574 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.088 seconds in which 0.039 seconds were spent on a total of 13 queries. Zlib compression enabled.

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