Board index » delphi » How do you define global variables/objects?

How do you define global variables/objects?

Is it possible to define a global variable/object that is to be
initilialised in  the 'program'  file and used by all units within
the application??

I thought declaring stuff under 'var' in the 'program' file
would mean that it would be global for the application, but
using the global in other units causes the compiler to error
and state that it is not identified.

 

Re:How do you define global variables/objects?


Quote
In article <48o1lk...@newsgate.dircon.co.uk>, sh...@dircon.co.uk (Shabir Sattar) writes:
> Is it possible to define a global variable/object that is to be
> initilialised in  the 'program'  file and used by all units within
> the application??

> I thought declaring stuff under 'var' in the 'program' file
> would mean that it would be global for the application, but
> using the global in other units causes the compiler to error
> and state that it is not identified.

Any variable declared in the interface part of any unit is globally accessible,
which I think is the most important feature of a global variable.  It can be
used in another unit simply by adding its defining unit to the uses statement
of the using unit.
Sincerely, R. Bullock

Re:How do you define global variables/objects?


Surely the simplest way would be to put all global variables in one
unit, and then have that unit in all necessary USES clauses.

Just thinking aloud, but it should be OK.

Quote
Pierre Vannieuwerburgh <Pierre.Vannieuwerbu...@ping.be> wrote:
>This is a multi-part message in MIME format.

>---------------------------------2402305815251
>Content-Transfer-Encoding: 7bit
>Content-Type: text/plain; charset=us-ascii

>There is a way to access in unit2 variables declared in unit1 : first
>create a uses clause after the implementation line and then include the
>unit name in it. Causes no compile error and works fine. I must say that
>passing variables via the clipboard looks a very complicated mechanism
>since there's one quiet more simple.

>I just hope it will help.

>---------------------------------2402305815251
>Content-Transfer-Encoding: 7bit
>Content-Type:
>---------------------------------2402305815251--

Re:How do you define global variables/objects?


In article <48o1lk...@newsgate.dircon.co.uk>, sh...@dircon.co.uk (Shabir Sattar) says:
Quote

>Is it possible to define a global variable/object that is to be
>initilialised in  the 'program'  file and used by all units within
>the application??

>I thought declaring stuff under 'var' in the 'program' file
>would mean that it would be global for the application, but
>using the global in other units causes the compiler to error
>and state that it is not identified.

 I have had a similar problem in which I declared
form 2 in form1's uses clause and declared string
variables in the public section of form1. When I passed
data to the string in unit1 and then tried to use
the string in unit2, it was not identified.
        It seems that to use the data in unit1 while
in unit2 I would have to declare unit1 in unit2's uses section.]
Since unit1 declares unit2 in unit1's uses area
such a declaration was disallowed. Also declaring Unit1
in the uses section of the implementation area of Unit2
would not solve the problem.
        I was trying to select filenames in Unit1 and
draw graphs of the binary files selected using the onpaint
section of a paint box. On click would not work and permit
scrolling. I finally had to use window's clipboard to pass
the data strings containing filenames to unit2.
        There was no alternative but to declare unit2 in
the unit1 uses section because I was using autocreate methods
for initializing the second form (unit2). I would like to hear
other people's solution to this type of problem. There seems
to be a serious problem communicating between units which
public declarations do not solve.
        If you need code for passing strings through clipboard
Reply to this and I'll post an example Monday.
(Code clips are at the office and I'm home for holidays).

Mike M...@neosoft.com

Re:How do you define global variables/objects?


In article <497i19$...@ping1.ping.be>, Pierre Vannieuwerburgh <Pierre.Vannieuwerbu...@ping.be> says:

Quote

>There is a way to access in unit2 variables declared in unit1 : first
>create a uses clause after the implementation line and then include the
>unit name in it. Causes no compile error and works fine. I must say that
>passing variables via the clipboard looks a very complicated mechanism
>since there's one quiet more simple.

>I just hope it will help.

Pierre, Thank you for taking an interest in this thread.
I have tried something similar to what you have described
and had no success with that approach. I suspect there is
some other convention I am neglecting which prevents this
from working for me. I am wondering if I need to declare
the variables in unit1 public area and again in unit2's
public area in a form as follows.
var unit1.mystring : string;
Would appreciate hearing any other comments you might
have on this matter. Thanks Mike    m...@neosoft.com

Other Threads