Board index » delphi » How do I define a Global constant ?

How do I define a Global constant ?

Hi

I would like to define a 'global' constant that was visible in every unit of
my application.

At the moment I am doing it like this :

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ExtCtrls, StdCtrls, Splash, jpeg;

const
  filepath = 'E:\Application\data\';

type
  etc etc .........

public
   filepath : string;
-----------------------------------------------

Is this right.....

I obviously have to define it using CONST. I also have to make it public to
be seen by the rest of the application so it needs declaring within PUBLIC
as a string type. When I come to make reference to it in other parts of the
application using TfmMain.filepath its not set.  I then move the CONST
declaration to below the IMPLEMENTATION
section but its still empty when called.  I know this is pretty basic but
whats the correct way to do this ??

Bruce.

 

Re:How do I define a Global constant ?


One way I like to do this, is to create a new unit (not form) and define
global constants, variabels, and functions/procedures in this unit. Then
add this units name in the Uses clause of all the units that are using the
constants, variabels, and functions/procedures.

BWM <b...@globalnet.co.uk> wrote in article
<6rogfi$35...@heliodor.xara.net>...

Quote
> Hi
> I would like to define a 'global' constant that was visible in every unit
of
> my application.
> Bruce.

Re:How do I define a Global constant ?


Quote
>At the moment I am doing it like this :

>unit Main;

>interface

>uses
>  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
>  ComCtrls, ExtCtrls, StdCtrls, Splash, jpeg;

>const
>  filepath = 'E:\Application\data\';

>type
>  etc etc .........

>public
>   filepath : string;
>-----------------------------------------------

>Is this right.....

The problem you are seeing is because you have two different things here.  You
have a constant called filepath and a property of the form called filepath.
The way you have these declared make them two separate things.

To reference the Constant from another form  simply use the name of the
constant Filepath do not use tfmMain.filepath.  Another proplem you may not
have encountered yet is if you reference filepath inside a method of tfmMain
you will only find the property and the Constant will not be found.

To avoid the confusion  which you are causing yourself never give a property
the same name as a constant.

I hope this helps.

Michael Hying

Re:How do I define a Global constant ?


Hi, All!

Quote
BWM wrote:
> Hi

> I would like to define a 'global' constant that was visible in every unit of
> my application.

> At the moment I am doing it like this :

> unit Main;

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
>   ComCtrls, ExtCtrls, StdCtrls, Splash, jpeg;

> const
>   filepath = 'E:\Application\data\';

> type
>   etc etc .........

> public
>    filepath : string;
> -----------------------------------------------

> Is this right.....

> I obviously have to define it using CONST. I also have to make it public to
> be seen by the rest of the application so it needs declaring within PUBLIC
> as a string type. When I come to make reference to it in other parts of the
> application using TfmMain.filepath its not set.  I then move the CONST
> declaration to below the IMPLEMENTATION
> section but its still empty when called.  I know this is pretty basic but
> whats the correct way to do this ??

> Bruce.

First:
  You SHOULD NOT!!! declare this constant in Your form class.
Second:
  Unit and any class in it are not the same.
Third:
  If You whant to get this constant just put unit Main into uses clause of
  current unit and USE IT!( without any problems, write only constant name,
  or, if there are different constants in different units, write
UNITNAME.CONSTNAME)
Fourth:
  The best way is to create new, special unit for different constants,
  declare all constants there in INTERFACE part, and use this unit
  whenever You need any constant

---------
Sincerely,
Vadim Gurevich
M&M Data Systems, Ukraine
e-mail: va...@mmdata.kharkov.ua

Re:How do I define a Global constant ?


In article <6rogfi$35...@heliodor.xara.net>, BWM <b...@globalnet.co.uk>
writes
Quote
>Hi

>I would like to define a 'global' constant that was visible in every unit of
>my application.

[cut]

I always reckoned the easiest way was to create a code module that
contained nothing other than global constants/variables, and include
that module in the USES clause of all your other modules.

There may be a cleaner way but that one always works for me :)

HTH

--
J A Baker

Other Threads