Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I sure hope this is cool, coz it took its time
#21
Don't use global vars/arrays. Wink
Reply
#22
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?
Reply
#23
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.

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
Reply
#24
How does this code solve my problems, and nevermind I solved it, I figure out that if I would go something like:

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.
Reply
#25
Those aren't global variables... They get passed by descriptor. Wink

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. Wink
Reply
#26
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???
Reply
#27
I always thought it was dim shared :???:
Reply
#28
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.
Reply
#29
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. Wink
Reply
#30
So we were both right, yay, I was right about something I hadn't yet tested.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)