<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi John<br>
<br>
While all you suggest may be true (I don't know about the compiler
setting stuff) I would strongly recommend anybody starting out with
programming / delphi avoids using global variables. Its a nasty
habit to get into :) <br>
<br>
Also the compiler setting you talk about sounds dangerous as someone
who didn't know about it could become highly confused looking at the
code or trying to compile it on a fresh install of Delphi !<br>
<br>
Also note I changed the typo spelling of the subject because it was
annoying me :)<br>
<br>
Cheers<br>
Rob<br>
<br>
On 21/01/2011 9:04 a.m., John Bird wrote:
<blockquote cite="mid:E6330840343544D49DB6D67B5F6A3380@JohnSony2"
type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<meta name="GENERATOR" content="MSHTML 8.00.6001.18999">
<style></style>
<div dir="ltr">
<div style="font-family: 'Arial'; color: rgb(0, 0, 0);
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> </div>
<div><tt><font style="font-size: 12pt;">var</font></tt></div>
<div><tt><font style="font-size: 12pt;">string3: string;</font></tt></div>
<tt><font style="font-size: 12pt;">
</font></tt>
<div><tt><font style="font-size: 12pt;"><br>
<br>
procedure TForm11.Button1Click(Sender: TObject);<br>
begin<br>
MyString := 'Hello, world!';</font></tt></div>
<div><tt><font style="font-size: 12pt;"> <font size="2">string2:=’Hello
world2’;</font></font></tt></div>
<div><tt><font style="font-size: 12pt;"> <font size="2">string5:=’Hello
world5’;</font><br>
end;</font></tt><br>
<br>
</div>
<div> </div>
<div><font face="Courier New">unit Storeunit;</font></div>
<div> </div>
<div><font face="Courier New">interface</font></div>
<div> </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> </div>
<div><font face="Courier New">implementation</font></div>
<div> </div>
<div><font face="Courier New">end.</font></div>
<div> </div>
<div style="font-family: 'Arial'; color: rgb(0, 0, 0);
font-size: 10pt;">John</div>
<div style="font-family: 'Arial'; color: rgb(0, 0, 0);
font-size: 10pt;">
<div style="font: 10pt tahoma;">
<div style="background: none repeat scroll 0% 0% rgb(245,
245, 245);">
<div style=""><b>From:</b> <a moz-do-not-send="true"
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 moz-do-not-send="true"
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> </div>
</div>
<div style="font-style: normal; display: inline; font-family:
'Calibri'; color: rgb(0, 0, 0); font-size: small;
font-weight: normal; text-decoration: none;">
<div><font face="Arial" size="2">Is there a way to store
variables so I can use them from one procedure to
another? </font></div>
<div> </div>
<div><font face="Arial" size="2">I have been currently
storing them in hidden edit.text boxes on the form but
there must be a better way. </font></div>
<div> </div>
<div><font face="Arial" size="2">Cheers Wallace</font></div>
<hr>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a class="moz-txt-link-abbreviated" href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</a><br>
Admin: <a class="moz-txt-link-freetext" href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a class="moz-txt-link-abbreviated" href="mailto:delphi-request@delphi.org.nz">delphi-request@delphi.org.nz</a>
with Subject: unsubscribe</div>
</div>
</div>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: <a class="moz-txt-link-abbreviated" href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</a>
Admin: <a class="moz-txt-link-freetext" href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a>
Unsubscribe: send an email to <a class="moz-txt-link-abbreviated" href="mailto:delphi-request@delphi.org.nz">delphi-request@delphi.org.nz</a> with Subject: unsubscribe</pre>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<p class="avgcert" color="#000000" align="left">No virus found in
this message.<br>
Checked by AVG - <a moz-do-not-send="true"
href="http://www.avg.com">www.avg.com</a><br>
Version: 10.0.1191 / Virus Database: 1435/3392 - Release Date:
01/20/11</p>
</blockquote>
</body>
</html>