Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Are Global Varibles Evil?

Status
Not open for further replies.

quebasic2

Programmer
Dec 17, 2002
174
IN
Anyone who has done any c, cpp, c# has been warned about the evils of using global varibles. You should not use them period according to most books. My question is:
If you are not using them to excess, are they consider bad programming in Qbasic?
 
Not necessarily evil.

I'm currently working on a customized VB DB program. I've ended having to use 4 global variables. Three are standard trash/garbage containers; and 1 boolean to check if the data (aka "record") has been changed and prompt 'saving' before continuing to next screen/procedure/module/etc.)

I'd suggest if you are going to use global variables in QB you place them at the top of the main module for easier referal. This does not preclude the fact that you should abandon local variables by any means. Simply that if you have to use them ... well then use them.

--MiggyD
 
Sorry, first line should have read:

"Not necessarily bad programming."

--MiggyD
 
There is nothing wrong with Global Vars...

They are handy for timers, switches, and many other things...

Just don't get carried away... They CAN be a mess to clean up at times if you change your mind...

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
CubeE101 just said:
>There is nothing wrong with Global Vars...
>They are handy for timers, switches, and many other things...

>Just don't get carried away..

I think this equally well could be applied to GOTO statement.

BASIC gives you a tool; you can use it wisely - or just other way around.
 
I agree with tsh73...

There are times where speed is an issue and you can use Goto statments for speed in place of loops...

But GOTO was one of the original commands from the Line Number Days (ughhh...)...

Since then, there have been new loops added, such as DO:LOOP

And SUBs and FUNCTIONs are much easier to deal with and seperate global and local variables than GOTO/GOSUB statements...

Basically Nothing is evil or good in Basic Syntax...
The trick is to KNOW when and where to use what...
You don't want to use the same commands and methods every where...
And knowing that comes with practice and experience...

Another context effective set of commands are...
Select Case:Case:Case Else:End Select
and
If Then:Else:End If

Say you use A$ = Inkey$ to trap a Key...

you might use If Then Else if you only have one key to watch for...
A$ = Inkey$
If A$ = Chr$(27) Then End


You Might Use Select Case in places where you check for multiple keys...
A$ = Inkey$
Select Case A$
Case "A"
Print "A"
Case "B"
Print "B"
Case ""
Case else
Print "NOT A or B"
end select


And in some cases you might use an equation in place of a if then / case select...
If A$=Chr$(0) + Chr$(72) then Y = Y - 5
If A$=Chr$(0) + Chr$(80) then Y = Y + 5


Can be replaced with this...
Y = Y + (-5 * (A$ = Chr$(0) + Chr$(72)))
Y = Y - (-5 * (A$ = Chr$(0) + Chr$(80)))


Then simplified to...
Y = Y - 5 * (A$ = Chr$(0) + Chr$(72))
Y = Y + 5 * (A$ = Chr$(0) + Chr$(80))


Good Luck

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top