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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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
ptmcompCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.