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

Reply via email to