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!

Declare a global variable 2

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
This may sound a little silly, but I can't figure out how to declare a global variable (public) to be made available to all forms in an application.

In VB.NET you simply add a module and declare alll your public variables...How do I do this in delphi so all forms can use the same public variable.

Thanx.




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
this is very simple :


unit u_globals;

interface

var
var1: integer;
var2: string;
...

implementation
end.



to make a small remark : normally global vars are not often used in delphi, it's better to maintain your variables inside the objects you use. something like this :

--------------
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
privatevar1 : integer;
public
{ Public declarations }
publicvar : integer;
end;

var
Form1: TForm1;
------------------

you see that only form1 is declared as global var. this way you can "assign" your variables to the relevant object and if the object is destroyed, so are your variables, this IMHO more memory efficient and easier to "understand" ,I hate having like 200+ global variables and after a year, not knowing what for/or where they ared used.

greetings,


 
Put your global variables in a datamodule, together with other non-visual components like Imagelists, tables, queries, actionlists etc...

Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top