Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 135159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:44:36+00:00 2026-05-11T06:44:36+00:00

I don’t know OWNER object class name. So I must check everywhere in my

  • 0

I don’t know OWNER object class name. So I must check everywhere in my codes like that :

if TObject(OWNER) is TFirstClass then begin   TFirstClass(OWNER).FirstFunction;   TFirstClass(OWNER).SecondFunction;   ... end else if TObject(OWNER) is TSecondClass then begin   TSecondClass(OWNER).FirstFunction;   TSecondClass(OWNER).SecondFunction;   ... end; 

Is there a better way? Because I must do this if condition in many place of the codes. All functions of TFirstClass and TSecondClass (which I have to run) are the same.

Note : I use Delphi 5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:44:36+00:00Added an answer on May 11, 2026 at 6:44 am

    If you have no access to TFirstClass and TSecondClass, but still want to simplify your code, here’s a way:

    Create an adapter base class:

    type   TMyAdapter = class(TObject)   public     procedure FirstMethod; virtual; abstract;     procedure SecondMethod; virtual; abstract;   end; 

    Then create descendant classes TFirstClassAdapter and TSecondClassAdapter and give them each a private reference to the instance of TFirstClass or TSecondClass respectively. Add a constructor which sets this reference. Override the methods of the adapter classes, so that they call through to the adapted classes.

    type   TFirstClassAdapter = class(TMyAdapter)   private     fObject: TFirstClass;   public     constructor Create(AAdaptedObject: TFirstClass);      procedure FirstMethod; override;     procedure SecondMethod; override;   end;  constructor TFirstClassAdapter.Create(AAdaptedObject: TFirstClass); begin   inherited Create;   fObject := AAdaptedObject; end;  procedure TFirstClassAdapter.FirstMethod; begin   fObject.FirstMethod; end;  procedure TFirstClassAdapter.SecondMethod; begin   fObject.SecondMethod; end; 

    Same for the other class. Now you only need to decide whether you create the adapter once and pass it around, or whether you make a function that you call everywhere you need it, and which will give you an adapter for your concrete class.

    If you implement the adapter using interfaces, then you will not even need to manage the lifetime of the adapter yourself.

    This way you can have the polymorphic behaviour that Ulrich gave in his answer, but without the need to change TFirstClass and TSecondClass.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if this has been asked before, but what i'd like to
Don't know if there is a better way to do this, so that is
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
I don't know when to add to a dataset a tableadapter or a query
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know how to explain it better but i'm trying to get a response

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.