Re: What's the proper way to share an object with several objects? (eg. a logging object)
- From: "Tom de Neef" <tdeneef@xxxxxxxx>
- Date: Wed, 27 Jul 2005 22:07:54 +0200
<jklimek@xxxxxxxxx> schreef in bericht
news:1122488420.959285.166100@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> This seems to be a stupid question but I'm a bit confused...
>
> Right now I have my main form that creates an object of the class
> "TLog". Main also creates other objects (eg. TConnections, TPlayer)
> that I would like to use the log object I just created.
>
> Do I need to pass my already-created TLog object into every other
> object I create? It seems like there is a better way of doing it.
>
> Example:
>
> 1. My main form creates a TLog and a TConnections object.
> 2. My TConnections object creates a TPlayer object.
> 3. My TPlayer object creates a TDatabase object.
>
> Do I need pass my TLog object into TConnections and then have it pass
> it to my TPlayer object and then finally pass it to my TDatabase
> object?
>
> One solution I COULD do is create seperate log objects but that would
> prevent me from logging to the screen (eg. I have a memo box on my main
> form that displays logging information so my TConnections object has no
> idea about this unless I pass an already created log object)
>
In particular for a log, which you might want to access from many places, I
would declare it in a unit that can be used by all others (like a GLOBALS
unit). Along these lines:
unit globals;
interface
type
Tlog = class
memo : Tmemo;
outputFile : textfile;
procedure SetupFile(filename : string);
procedure SaveFile;
procedure Show(txt : string);
...
end;
var
myLog : Tlog;
implementation
...
procedure Tlog.show(txt: string);
// display on memo and write to file
begin
memo.lines.add(txt);
writeln(outputFile,txt);
end;
initialization
myLog:=Tlog.create;
myLog.setUpFile('appLog.txt'); // for instance
finalization
myLog.saveFile;
end;
In the main form you then associate the memo:
myLog.memo:={mainForm.}memo1
so that all output procedures can be coded as methods of Tlog, as the show
method above.
Tom
.
- Prev by Date: Re: Changing the color of the active title bar
- Next by Date: Re: Changing the color of the active title bar
- Previous by thread: Changing the color of the active title bar
- Next by thread: How Restore Docking on Undocked Window Close
- Index(es):