Polymorphism?

Can I possibly convert this code:

           if parent.Components[ i ] is Tmemo then
           begin
                With parent.components[i] as Tmemo do
                begin
                     left:=safestrtoint(Strs.strings[1]);
                     top:=Safestrtoint(strs.strings[2]);
                     height:=safeStrToInt(Strs.Strings[3]);
                     width:=safeStrToInt(Strs.Strings[4]);
                end;
           end else
           if parent.Components[ i ] is TComboBox then
           begin
                With parent.components[i] as TComboBox do
                begin
                     left:=safestrtoint(Strs.strings[1]);
                     top:=Safestrtoint(strs.strings[2]);
                     height:=safeStrToInt(Strs.Strings[3]);
                     width:=safeStrToInt(Strs.Strings[4]);
                end;
           end else
           if parent.Components[ i ] is TImage then
           begin
                With parent.components[i] as TImage do
                begin
                     left:=safestrtoint(Strs.strings[1]);
                     top:=Safestrtoint(strs.strings[2]);
                     height:=safeStrToInt(Strs.Strings[3]);
                     width:=safeStrToInt(Strs.Strings[4]);
                end;
           end;
(etc)

into a one-off procedure?  rather than repeating the code for each type... thanks!
LVL 1
delphiexpertAsked:
Who is Participating?
 
ptmcompConnect With a Mentor Commented:
What's about using the SetBounds method:

procedure Setbounds(ALeft, ATop, AWidth, AHeight: Integer); virtual;

Just try this:
Parent.Components[i].SetBounds(SafeStrToInt(Strs.Strings[1], SafeStrToInt(Strs.Strings[2], SafeStrToInt(Strs.Strings[3], SafeStrToInt(Strs.Strings[4]);

Regards, ptm.
0
 
AttarSoftwareCommented:
Can't you do:

  parent.components[i].left:=safestrtoint(Strs.strings[1]);
  parent.components[i].top:=Safestrtoint(strs.strings[2]);
  parent.components[i].height:=safeStrToInt(Strs.Strings[3]);
  parent.components[i].width:=safeStrToInt(Strs.Strings[4]);

?

Does than not work?

Hmmm..

Tim.
0
 
zwarteCommented:
Try this:

if (parent.Components[ i ] is TComboBox) or (parent.Components[ i ] is Tmemo ) or (parent.Components[ i ] is TImage ) then
With parent.components[i] as Tmemo do
Begin
parent.components[i].left:=safestrtoint(Strs.strings[1]);
  parent.components[i].top:=Safestrtoint(strs.strings[2]);
  parent.components[i].height:=safeStrToInt(Strs.Strings[3]);
  parent.components[i].width:=safeStrToInt(Strs.Strings[4]);
End;

you'll probably need to typecast (like I do here) to something that has a top,left,height and width 'cause else the compiler might complain, but I think this has a good shot on succes :-)

Tom;
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
XANCommented:
may be you find the next usefull:

  if (parent.Components[1] is Tmemo) or (parent.Components[1] is TComboBox) or (parent.Components[1] is TImage) then
  begin
    With parent.components[i] as parent.components[i].ClassType do
    begin
      left:=safestrtoint(Strs.strings[1]);
      top:=Safestrtoint(strs.strings[2]);
      height:=safeStrToInt(Strs.Strings[3]);
      width:=safeStrToInt(Strs.Strings[4]);
    end;
0
 
florisbCommented:
I heard using with works slow...

better
Tmemo(parent.components[i]).left := x
Tmemo(parent.components[i]).top := y

et cetera.


Just thinking: You use run-time generated controls? If there are connected in some way, you could consider the use if own versions of components, that you link in some way.

F.E.

TMySpeedButton = class(TSpeedButton)
  private
    FAssociate: TWinControl;
    procedure SetAssociate(const Value: TWinControl);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    property Associate: TWinControl read FAssociate write SetAssociate;
  published
    { Published declarations }
  end;

After the speedbutton is clicked, instead of looping all components, you connect them.

Floris.

0
 
delphiexpertAuthor Commented:
As the result of all your comments I ended up using this single line
           TControl(parent.components[i]).SetBounds(SafeStrToInt(Strs.Strings[1]), SafeStrToInt(Strs.Strings[2]), SafeStrToInt(Strs.Strings[4]), SafeStrToInt(Strs.Strings[3]));

Which is a lot better than all of those lines I originally had
Now... who should get the points?

0
 
AttarSoftwareCommented:
Heh...ME! (giggle) ;O)

Tim.
0
 
delphiexpertAuthor Commented:
Funny.. your solution doesn't even work :)  You need to typecast the parent.components[i] part at least.
0
 
AttarSoftwareCommented:
Worth a try =O)

0
 
zwarteCommented:
I dunno,
you figure it out who helped you the most.

I said typecasting which hopefully solved it

Xan added a better way to typecast

florish thought an extra list would do better

ptmcomp added setbounds to reduce codesize

and attarsoftware also gave it a try ;-)

I think I had everybody :
now choose for the big lot

Let the wheel spin Walter ....

(Sorry to many TV-shows for me :-)

Tom,
0
 
delphiexpertAuthor Commented:
Adjusted points to 30
0
 
delphiexpertAuthor Commented:
Ok well seeing as I came up with the TControl typecast on my own, ptmcomp's suggestion actually helped the most, even though it was wrong (missing right brackets, etc, but that was basically syntax)
So here you go ptmcomp
10 bonus points bringing it up to a massive 30.  Well, it wasnt that hard was it? :)
0
 
ptmcompCommented:
You're right, I didn't even try it, so I didn't realize that I need to typecast the Component[i] and there was also missing a bracket. What did we learn? - Always test your work before shipping #;-} .
Regards, ptm.
0
All Courses

From novice to tech pro — start learning today.