Just as a side not on this topic, Gerry wrote to me personally and I
was able to gather that the "real" problem was that he was evidently
attempting to use a library unit...one not part of his project...calling a
procedure that would in turn need to reference an object in his project!
And of course that's not a good idea because the library unit suddenly is no
longer re-useable if you do that.  The right way to handle this would be to
change the procedure to a function with a return value his project can use.
Then the library unit has no need to reference any unit of his project.  

>From "Robert Meek" 
Personal e-mail:  [EMAIL PROTECTED]
dba / "Tangentals Design"
Visit us at:  www.TangentalsDesign.com
Home of "The Keep"!

Member of:  "Association of Shareware Professionals"
Moderator for:  "The Delphi", "Delphi-DB", and "Delphi-Talk"
programming lists at elists.org, and "DelphiTalk.net"
at www.DelphiTalk.net


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Stephen Posey
Sent: Tuesday, March 15, 2005 6:30 PM
To: Borland's Delphi Discussion List
Subject: Re: Pointer Usage

Gerry Bendixen wrote:

> Obviously I was not so clear on my goals.
> Here's an attempt to provide an example, stripped of all non-essential
details.  Hopefully I present it correctly.
> I have a 'main' unit (Main.pas) with a form.  On the form is ProgressBar1
(a TProgressBar component)
> I have a 'uses' unit (Unit1.pas) that does processing and the goal is to
show the progress of this processing by updating the ProgressBar on the main
form.
> 
> (1) Type PProgressBar = ^TProgressBar ;  //Declare pointer type
> (2)  Var  ProgressBar1Ptr : PProgressBar ;  //Declare pointer variable
> (3)  ProgressBar1Ptr := PProgressBar(ProgressBar1) ;  //Assign value to
pointer
> (4)  TProgressBar(ProgressBarPtr).Position := CalculatedBarPosition ;

Ouch, that's kind of convoluted pointer usage, and really unnecessary 
with Delphi classes and objects.

> Line 1 and 2 are visible to both Main and Unit1.
> Line 3 is executed in Main to set the value to the pointer.
> Line 4 is executed in Unit1 is update the position of the progress bar.

That clarifies things a lot, thanks.

You can refer to the progress bar using any of the techniques I 
mentioned in my previous email. Probably the simplest being to refer to 
the progress bar directly using the MainForm instance variable:

MainForm.ProgressBar1.Position := CalculatedBarPosition;


As I mentioned previously though, I'm leery of directly accessing 
components on a form from code in another unit. It increases module 
coupling and breaks the encapsulation barrier.

My personal preference for a case like this would be either to add a 
method to MainForm from to update the progressbar and call that from the 
other code, e.g.:

// in Main.pas
type
   TMainForm = class(TForm)
     ProgressBar1 : TProgressBar;

   public
     procedure UpdateProgress(Value: Integer);
   end;

var
   MainForm: TMainForm;

implementation

TMainFon.UpdateProgress(Value: Integer);
begin
   ProgressBar1.Position := Value;
end;

...

// in Unit1.pas
uses
   Main;

...

MainForm.UpdateProgress(CalculatedBarPosition);


Or (possibly better) make UpdateProgress an exported function of the unit:

// in Main.pas
type
   TMainForm = class(TForm)
     ProgressBar1 : TProgressBar;

   public
   end;

procedure UpdateProgress(Value: Integer);

var
   MainForm: TMainForm;

implementation

TMainFon.UpdateProgress(Value: Integer);
begin
   MainForm.ProgressBar1.Position := Value;
end;

...

// in Unit1.pas
uses
   Main;

...

UpdateProgress(CalculatedBarPosition);



In this way you encapsulate "progress updating" to a single place local 
to where it occurs.  That allows you to easily change how progress is 
displayed or include other things that may need to be updated at the 
same time.

For the latter case, you may want to add some code to make sure MainForm 
is instantiated, and do so if it isn't.

HTH

Stephen Posey
[EMAIL PROTECTED]



_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to