Board index » delphi » Q:Global variables??

Q:Global variables??

Hi folks!

I VB there is possible to make a variable global in a whole program by
declaring it global in a module.
Is there a way to do the same thing in delphi (without doing Uses unit2
in unit1 usw)?

Thanx
Stefan

 

Re:Q:Global variables??


Quote
Stefan <Stefan.arvids...@hexagon.se> wrote:
>Hi folks!
>I VB there is possible to make a variable global in a whole program by
>declaring it global in a module.
>Is there a way to do the same thing in delphi (without doing Uses unit2
>in unit1 usw)?
>Thanx
>Stefan

If I ever need to declare global variables, I'll do one of two things.
The one that takes more effort would be to declare a VarsUnit. In it,
I'll declare types, constants, variables that I want globally defined,
and just include it in every form's uses statement. The other way with
less work is to open up your project's .dpr file in the editor. Since
this is the actual program that is run, you only have to declare the
types/variables/etc. in it, and they're treated globally as well.

 - FireHawk

Re:Q:Global variables??


In article <31C16A5C....@hexagon.se>, Stefan
<Stefan.arvids...@hexagon.se> writes

Quote
>Hi folks!

>I VB there is possible to make a variable global in a whole program by
>declaring it global in a module.
>Is there a way to do the same thing in delphi (without doing Uses unit2
>in unit1 usw)?

there is no way for this (I am 99% sure).  Anyway globals are bad,
locals are good 8-)
--
Nicholas Robinson

Re:Q:Global variables??


Quote
FireHawk wrote:

> If I ever need to declare global variables, I'll do one of two things.
> The one that takes more effort would be to declare a VarsUnit. In it,
> I'll declare types, constants, variables that I want globally defined,
> and just include it in every form's uses statement. The other way with
> less work is to open up your project's .dpr file in the editor. Since
> this is the actual program that is run, you only have to declare the
> types/variables/etc. in it, and they're treated globally as well.

>  - FireHawk

Hi & Thanx Firehawk.

Just one extra quest. Have U tried and succeded with the model in which
U declare a global variable in the dpr-file? I've tried it and the
compiler buys the declaration but....when it reaches the actual variable
in an event handler it stops and cries "undeclared identifier". Must i
treat the global one in some special way or what??

once again: ThankYou for lending me Ur time.

//Stefan

Re:Q:Global variables??


Quote
Stefan <Stefan.arvids...@hexagon.se> wrote:
>[snip]
>Just one extra quest. Have U tried and succeded with the model in which
>U declare a global variable in the dpr-file? I've tried it and the
>compiler buys the declaration but....when it reaches the actual variable
>in an event handler it stops and cries "undeclared identifier". Must i
>treat the global one in some special way or what??
>once again: ThankYou for lending me Ur time.
>//Stefan

Its no ptoblem at all. I do have a solution for the dpr-declaration...
sort of. It's more a combination of the two. Since the compiler won't
let you reference a variable till it's declared, the straight
declaration won't cut it unless you can "trick" the compiler into
taking it (which I'm not sure, can be done). It's been a while since
I've needed global variables, and I'm not sure if this solution would
work, but instead of declaring the one VarsUnit in every other unit,
try declaring the VarsUnit first in the dpr file. If Pascal acts the
same way C++ does in respect to units and #includes, then everything
should be hunky-doory. If not, well, you'll just have to declare it
over and over again...

 - FireHawk

Other Threads