Page 1 of 1 1
Topic Options
#78723 - 2002-02-13 12:50 AM Using global to declare an array.
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
Hey guys, i think i found a bug. Kix 4.02

you can not use Global to make a variable in to an array if that variable was already defined.

code:

$temp = 0
? "vartype = " vartype($temp)

;the next line will fail
global $temp[10]
? "vartype = " vartype($temp)


but you do not have this problem with DIM

code:

$temp = 0
? "vartype = " vartype($temp)

;$temp is redefined as an array
dim $temp[10]
? "vartype = " vartype($temp)


The reason I want to use Global Vs Dim is that this variable must exist outside the confines of the UDF it was created in.

Top
#78724 - 2002-02-13 02:29 PM Re: Using global to declare an array.
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Bryce,
hmmm ... seems logical to me behaving like shown by u

on the other side you could also define that by doing this (simplified):


$temp = yourfunction()

function yourfunction()
    dim $temp[20]
    $yourfunction = $temp
endfunction


(it will get a global variable/array)

hth

J.

[ 13 February 2002: Message edited by: jpols ]

[ 13 February 2002: Message edited by: jpols ]

_________________________



Top
#78725 - 2002-05-01 07:40 PM Re: Using global to declare an array.
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
this is still a problem with kix 4.10beta

this code will cause kix to error out.
code:
$array = 1
global $array[10]

but the DIM command does not have this problem.

code:
$array = 1
dim $array[10]

Bryce

Top
#78726 - 2004-06-15 10:49 PM Re: Using global to declare an array.
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
been a while... kix4.22 this behavior is still here...

Code:

$array = 1
global $array[10]
? ubound($array)



ubound for $array returns -1

Top
#78727 - 2004-06-23 03:23 PM Re: Using global to declare an array.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
This is expected behaviour, surely?

You've attempted to declare the same global variable twice - once implicitly by assigning a value and once explicitly by using "global".

The only real surprise is that the explicit declaration does not produce an error - note, if you have SetOption("Explicit","ON") then duplicate declarations do produce an error - another good reason for always setting the option.

When you use "Dim" to create the array you are not using the same variable - you are creating a new variable in the local scope.

The local variable will of course hide the global variable, so it will appear to have "worked".

This scriptlet shows what I mean:
Code:
; Create an instance of $array in the global scope

$var = 1

; Now create an instance of $array in the local scope. This will hide the
; global instance.
dim $var[10]

"In local scope the variable is "+VarTypeName($var)+@CRLF
foo()

Function foo()
"In global scope the variable is "+VarTypeName($var)+@CRLF
EndFunction



If you want to convert the global to an array, you will need to use ReDim, or assign it from a function return value.

So long as you haven't create a local scope version of the variable, the following will work for you:
Code:
$array = 1

ReDim $array[10]
? ubound($array)


Top
#78728 - 2004-11-02 11:51 PM Re: Using global to declare an array.
GillRilla Offline
Lurker

Registered: 2004-11-02
Posts: 1
I started Kix'cripting 2 days ago and wanted to say thanks guys for your indirect help on my issue using an array within a UDF.

My code didn't seem to work until I defined my array variable as Global. Example below:

Break On
Global $LogArray[100]

$X = 0
$LogFile = "C:\KixLogFile.txt"

SendToLog("This is Line 1.")
SendToLog("This is Line 2.")

WriteToLog()
Quit

Function SendToLog($LogEntry)
$LogArray[$X] = $LogEntry + @CRLF
$X = $X + 1
EndFunction

Function WriteToLog()
Del $LogFile
If Open(1, $LogFile, 5) = 0
For $index = 0 to ($X -1)
WriteLine(1, $LogArray[$index])
Next
EndIf
$ = Close(1)
EndFunction

Top
#78729 - 2004-11-03 01:07 AM Re: Using global to declare an array.
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It is expected behavior and unrelated to this thread so I am closing it.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, 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.067 seconds in which 0.036 seconds were spent on a total of 12 queries. Zlib compression enabled.