12-10-2005, 12:59 AM
12-10-2005, 02:24 AM
Why shouldn't I use global vars/arrays, what if the var/array is used in 2 seperate routines, the you would have to make it global would you not?
12-10-2005, 04:09 AM
It's just bad practice. You can easily send an array to a sub like this...
By the way, it's FB code because of the underscores in the sub names, so it wont run in QB.
By the way, it's FB code because of the underscores in the sub names, so it wont run in QB.
Code:
Declare Sub Print_Data( tArray() As Integer )
Declare Sub Fill_Array( tArray() As Integer )
Dim Array(1 To 500) As Integer
Fill_Array( Array() )
Print_Data( Array() )
Sleep
'All of your subs can be way down here, out of the way, or even in a different file.
Sub Print_Data( tArray() As Integer )
Dim i
For i = Lbound(tArray) To Ubound(tArray)
Print tArray(i)
Next
End Sub
Sub Fill_Array( tArray() As Integer )
Dim i
For i = Lbound(tArray) To Ubound(tArray)
tArray(i) = Int(Rnd*1000)
Next
End Sub
12-10-2005, 09:32 PM
How does this code solve my problems, and nevermind I solved it, I figure out that if I would go something like:
Then when I want to fill somewhere I would simply go"
Now the x% and y% are the current position and the colour% is... the color, duh.
So I use 3 global vars, and it works fine. Now if you want to say that is bad practive go ahead and give me a better way to use the x%, y%, and colour% in otehr SUB's a better way.
Code:
declare sub fill(x AS INTEGER, y AS INTEGER,c AS INTEGER)
sub fill(x AS INTEGER, y AS INTEGER,c)
if point(x,y)> 0 THEN EXIT SUB
PSET(x,y),c
fill(x-1,y,c)
fill(x,y+1,c)
fill(x+1,y,c)
fill(x,y-1,c)
end sub
Then when I want to fill somewhere I would simply go"
Code:
IF inkey$ = "f" OR inkey$ = "F" then
fill(x%,y%,colour%)
end if
Now the x% and y% are the current position and the colour% is... the color, duh.
So I use 3 global vars, and it works fine. Now if you want to say that is bad practive go ahead and give me a better way to use the x%, y%, and colour% in otehr SUB's a better way.
12-11-2005, 05:01 AM
Those aren't global variables... They get passed by descriptor.
Once you are inside the sub, the variable names can use the same characters as those outside the sub, without screwing up your code. That's why I never use global variables.
Once you are inside the sub, the variable names can use the same characters as those outside the sub, without screwing up your code. That's why I never use global variables.
12-11-2005, 08:22 AM
That's what you mean, well then I guess I don't use Global variables, I guess Global variables are those defined as CONST right???
12-11-2005, 08:28 AM
I always thought it was dim shared :???:
12-11-2005, 08:32 AM
I'm not sure, that's why I added ??? to the end, to show I'm not entirely sure, I guess they'd both be considered Global since they use the same values in and out of SUB's/FUNCTIONS's.
12-11-2005, 10:05 AM
Yeah, DIM SHARED creates a global variable. CONST stands for constant, which means it keeps a constant value throughout the program. You'll usually find things like Pi, Gravity, key defines, etc... as constants.
12-11-2005, 10:21 AM
So we were both right, yay, I was right about something I hadn't yet tested.