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

  • SEARCH
  • Home
  • 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 6045091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:05:10+00:00 2026-05-23T07:05:10+00:00

I would like to have some suggestions for the following problem: Let’s say you

  • 0

I would like to have some suggestions for the following problem:
Let’s say you want to write adapters for the VCL controls. All Adapters should have the same base class, but differ in wrapping special controls (for example getting a value from a TEdit is different from getting a value from TSpinEdit).
So the first idea is to create a class hierarchy like

TAdapter = class
end;

TEditAdapter = class (TAdapter)
end;

TSpinEditAdapter = class (TAdapter)
end;

Now I want to introduce a field to hold a reference to the vcl control. In my special adapaters I want – of course – work with the concrete subclass. But the Base class should also contain a reference (for example if I want to use the adapter to make a control visible).

Possibility 1 (Downcast in Property Accessor):

TAdapter = class
protected
  FCtrl : TControl;
end;

TEditAdapter = class (TAdapter)
  public
    property Control : TEdit read GetControl write Setcontrol;
end;
{...}
function TEditAdapter.GetControl : TEdit;
begin
  Result := FCtrl as TEdit;
end;

So if I implement a specific method I work with the property Control, if I do something in my base class I use the protected field.

Possibility 2 (Use a generic base class):

TAdapter = class
end;

TAdapter <T : TControl> = class (TAdapter)
protected
  FCtrl : T;
end;

TEditAdapter = class (TAdapter <TEdit>)
end;

Which solution would you prefer? Or is there a third solution, which is even better?

Kind regards,

Christian

  • 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. Editorial Team
    Editorial Team
    2026-05-23T07:05:11+00:00Added an answer on May 23, 2026 at 7:05 am

    You can’t use generics to solve this problem, because you’ll be in one of two situations:

    • The property or method you want to “Adapt” (the Text property for example) is defined in an ancestor class. In that case you don’t need generics because you can use the one adapter for the ancestor and solve the problem for all descendants.
    • The property or method is introduced by the class you want to adapt. In that case you can’t use generics because in order to access the property or method you’d need a generic type constraint of that given type. Example. Let’s say you want an adapter for the Text property of TMyClass. Let’s assume TMyClass is the one introducing the Text property. In order to access it, you’d need to declare the generic type as TGeneric<T:TMyClass> and that’s not actually generic.

    In my opinion the best bet is to write specific adapters for each class, as in your first option. You might be able to use RTTI tricks to make your first option easier to implement, but I’m not sure it’d be worth it.

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

Sidebar

Related Questions

I would like to have some functionality by which if I write <textarea maxlength=50></textarea>
I would like to have some space between the same line.. I know there
I have some files in my SVN repository that I would like to have
I have some text in a UITextView, that I would like to have show
I have some files that I would like to rename using regex and powershell,
I have some data that I would like to visualize. Each byte of the
If you have some blocks of code that you would like to prevent execution
I have some data from log files and would like to group entries by
I have some Objective-C [i-os] code that I would like to run unit tests
I have some auto-instantiation code which I would like to apply to about 15

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.