<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.6001.18999">
<STYLE></STYLE>
</HEAD>
<BODY dir=ltr bgColor=#ffffff>
<DIV dir=ltr>
<DIV style="FONT-FAMILY: 'Arial'; COLOR: #000000; FONT-SIZE: 10pt">
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>You can take this further:</DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>Here string4 string5 integer1 integer2 integer3 can all be seen from
anywhere that uses Storeunit</DIV>
<DIV> </DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV>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.</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt">type<BR> TForm11 =
class(TForm)<BR> Button1: TButton;<BR>
procedure Button1Click(Sender: TObject);<BR> private<BR>
{ Private declarations }<BR> public<BR> { Public
declarations }<BR> MyString : string;<BR>
end;<BR><BR>var<BR> Form11: TForm11;</FONT></TT></DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt"> <FONT size=2>string2:
string;</FONT><BR><BR>implementation<BR></FONT></TT></DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt">uses storeunit;</FONT></TT></DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt"><BR>{$R *.dfm}</FONT></TT></DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt"
face="Courier New"></FONT></TT> </DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt">var</FONT></TT></DIV>
<DIV><TT><FONT style="FONT-SIZE: 12pt">string3: string;</DIV><FONT
size=2></FONT><FONT size=2></FONT><FONT size=2></FONT><FONT size=2></FONT><FONT
size=2></FONT><FONT size=2></FONT><FONT size=2></FONT><FONT size=2></FONT><FONT
size=2></FONT><FONT size=2></FONT>
<DIV><FONT size=2></FONT><FONT size=2></FONT><FONT size=2></FONT><FONT
size=2></FONT><FONT size=2></FONT><BR><BR>procedure TForm11.Button1Click(Sender:
TObject);<BR>begin<BR> MyString := 'Hello, world!';</DIV>
<DIV> <FONT size=2>string2:=’Hello world2’;</FONT></DIV>
<DIV> <FONT size=2>string5:=’Hello
world5’;</FONT><BR>end;</FONT></TT><BR><BR></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">unit Storeunit;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">interface</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">var</FONT></DIV>
<DIV><FONT face="Courier New">string4:string;</FONT></DIV>
<DIV><FONT face="Courier New">string5:string;</FONT></DIV>
<DIV><FONT face="Courier New">integer1:integer;</FONT></DIV>
<DIV><FONT face="Courier New">integer2:integer;</FONT></DIV>
<DIV><FONT face="Courier New">integer3:integer;</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">implementation</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New">end.</FONT></DIV>
<DIV> </DIV>
<DIV style="FONT-FAMILY: 'Arial'; COLOR: #000000; FONT-SIZE: 10pt">John</DIV>
<DIV style="FONT-FAMILY: 'Arial'; COLOR: #000000; FONT-SIZE: 10pt">
<DIV
style="FONT-STYLE: normal; DISPLAY: inline; FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: small; FONT-WEIGHT: normal; TEXT-DECORATION: none"><FONT
size=2 face=Arial></FONT></DIV>
<DIV style="FONT: 10pt tahoma">
<DIV style="BACKGROUND: #f5f5f5">
<DIV style="font-color: black"><B>From:</B> <A title=marshland@marshland.co.nz
href="mailto:marshland@marshland.co.nz">Marshland Engineering</A> </DIV>
<DIV><B>Sent:</B> Thursday, January 20, 2011 3:45 PM</DIV>
<DIV><B>To:</B> <A title=delphi@delphi.org.nz
href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</A> </DIV>
<DIV><B>Subject:</B> [DUG] Variabels stored</DIV></DIV></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV></DIV>
<DIV
style="FONT-STYLE: normal; DISPLAY: inline; FONT-FAMILY: 'Calibri'; COLOR: #000000; FONT-SIZE: small; FONT-WEIGHT: normal; TEXT-DECORATION: none">
<DIV><FONT size=2 face=Arial>Is there a way to store variables so I can use them
from one procedure to another? </FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>I have been currently storing them in hidden
edit.text boxes on the form but there must be a better way. </FONT></DIV>
<DIV><FONT size=2 face=Arial></FONT> </DIV>
<DIV><FONT size=2 face=Arial>Cheers Wallace</FONT></DIV><FONT size=2
face=Arial></FONT>
<HR>
_______________________________________________<BR>NZ Borland Developers Group -
Delphi mailing list<BR>Post: delphi@delphi.org.nz<BR>Admin:
http://delphi.org.nz/mailman/listinfo/delphi<BR>Unsubscribe: send an email to
delphi-request@delphi.org.nz with Subject:
unsubscribe</DIV></DIV></DIV></BODY></HTML>