There is another way as well, you can declare simple
global variables – depending where you declare it determines
it’s scope - how visible it it is.
In this example string2 can be seen by any unit that uses
this one, just as Form11 (the particular instance of
TForm11, and is also a global variable) is visible.
String3 can be seen by all procedures in this unit, but
not by anywhere else. If you have lots of simple
variables to store and they don’t need to be inherited etc
this is the simplest way to do it.
You can take this further:
If I have lots of constants to declare, or variables to
store I create a unit which is code only (no classes) eg
called storeunit and declare all the constants and variables
I want there, any form or unit that wants access to all
these variables just has to add storeunit to its uses
clause. This centralises all such declarations in one
place away from a form and is very tidy. I will often put
simple global functions and procedures in here too, as they
also become globally available, eg various standard ways for
formatting dates and strings. Also this unit can be uses
in different projects as well. For this just go to
File/New/Unit and the IDE gives you a new blank unit
already to add stuff to – a simpler unit with no form or
class stuff.
Here string4 string5 integer1 integer2 integer3 can all
be seen from anywhere that uses Storeunit
It depends on whether you like using global variables or
not. Also its a good idea to use a clear naming convention
for such variables.
There are other tricks you can do too - you can alter
compiler settings to allow assignable constants for a
procedure, then any values assigned here will be preserved
between calls to the procedure. But that seems to be
confusing to me, as it really becomes a variable and not a
constant. I saw that used in and example where the program
wanted a counter of the number of times the procedure was
called, and the counter constant in the procedure was
assigned a new value each time the procedure was called, its
quite a tidy way to do that sort of thing. In this case the
scope (visibility) of the variable is totally limited to the
one procedure.
type
TForm11 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyString : string;
end;
var
Form11: TForm11;
string2:
string;
implementation
uses storeunit;
{$R *.dfm}
var
string3: string;
procedure TForm11.Button1Click(Sender: TObject);
begin
MyString := 'Hello, world!';
string2:=’Hello
world2’;
string5:=’Hello
world5’;
end;
unit Storeunit;
interface
var
string4:string;
string5:string;
integer1:integer;
integer2:integer;
integer3:integer;
implementation
end.
John
Sent: Thursday, January 20, 2011 3:45 PM
Subject: [DUG] Variabels stored
Is there a way to store
variables so I can use them from one procedure to
another?
I have been currently
storing them in hidden edit.text boxes on the form but
there must be a better way.
Cheers Wallace
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post:
delphi@delphi.org.nz
Admin:
http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to
delphi-requ...@delphi.org.nz
with Subject: unsubscribe