"Alan Colburn" <[EMAIL PROTECTED]> wrote
>: try
> with AIniFile do
> begin
> WriteInteger('Config', 'Width', Form1.width);
> //something like a WriteString method to save Memo1's Text, or
> a series of WriteStrings to save each of Memo1's
WriteString ('Memo', .'Memo Text', Memo1.Text[1]);
and read it back like this
Memo1.Text := ReadString ('Memo', Text', '');
But I normally adopt a very different strategy when communicating
between forms.
1. When creating the second form, I pass a reference to the first
form. e.g.
Constructor TForm2.Create (aOwner: tComponent);
begin
fForm1 := aOwner;
inherited Create;
end;
2. Set up a reference to the second form as a private field of the
first form. e.g.
fForm2 := tForm2.Create (Self);
3. I set up properties for the forms to handle any communication
likely to be required to between the 2 forms. e.g.
property MemoText: string read memo1.text write memo1.text;
4. Set up a public procedure in the first form that sets the reference
to the second form to nil to prevent access violations from trying
to access a form that has been freed (or not yet created). e.g.
procedure TForm1.DeallocateForm2;
begin
fForm2:= nil;
end;
5. In the OnClose event of the second form, run the above public
procedure to notify the first form of the closure of the second
(and set the CloseAction of the second to caFree). e.g.
procedure tForm2.Close (var Action: tAction);
begin
Form1.DeallocateForm2;
Action := caFree;
end;
And any time I wish to access Form2 from Form1, I use
If Assigned (fForm2) then
fForm2.DoSomething;
This method
Avoids the use of globals
Avoids message passing;
Avoids writing files to disk.
HTH and does not confuse.
Henry Bartlett
email: hambar at microtech dot com dot au
----- Original Message -----
From: "Alan Colburn" <[EMAIL PROTECTED]>
To: "Borland's Delphi Discussion List" <[email protected]>
Cc: <[EMAIL PROTECTED]>
Sent: Wednesday, January 24, 2007 2:07 PM
Subject: RE: Scope and Message Passing
Hi Rob --
Thanks for taking the time to write such a lengthy and educational
response; I really appreciate it. The funny thing is, I'm still unsure
about what to pass when trying to facilitate communication between
classes--even if I'm OK with the difference between passing by value
and by reference, that TForm1 is descended from TForm, that the var
declaration (below) creates only memory space pointing to the address
of an instance of type TForm1, etc.
So, if it's OK with you, let me give you a silly situation, and ask
you about what the code would look like. I'll use default values
throughout, and start with a form, a memo, and a button. Unit1 would
include this code:
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Create a new class, TIni, to make the following two things happen.
When Form1 closes, the form's width and the memo's lines should be
saved to an ini file. (If it's easier to save Memo1.Text than
Memo1.Lines, that's OK.) I assume the procedure to write configuration
info to disk would include ...
procedure TIni.WriteConfig (???);
var
aIniFile: TIniFile;
begin
//writes user info to config.ini
AIniFile := TIniFile.Create(ExtractFilePath(Application.EXEName) +
'config.ini');
try
with AIniFile do
begin
WriteInteger('Config', 'Width', Form1.width);
//something like a WriteString method to save Memo1's Text, or a
series of WriteStrings to save each of Memo1's Lines
end;
finally
aIniFile.Free;
end;
end;
If there's other types of variables, references, etc. that are
commonly passed between objects, you might save a later posting from
me by mentioning them now :-)
Thank you again, and reply only if you find the time. But know that
your replies and those of others make a difference to me. If there's a
basic reference you think I ought to look at, feel free to mention it.
I'd love to take a class, but Delphi-based instruction seems to be
unavailable. -- Al
_________________________________________________________________
Type your favorite song. Get a customized station. Try MSN Radio
powered by Pandora.
http://radio.msn.com
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi