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 7924895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:00:38+00:00 2026-06-03T18:00:38+00:00

i want typecast using with class functions. i have base (TBase),derived (TDer) and typecasting

  • 0

i want typecast using with class functions.
i have base (TBase),derived (TDer) and typecasting (TMyType) class.

Ver : Delphi 7

TBase = class;
TDer = class;
TMyType = class;

TBase = class
  function Say : String;
  class function MYType:TMyType;
end;

TDer = class(TBase)
  a: string;
  b: string;
  function Say2 : String;
end;

TMyType=class(TBase)
  class function AsDer:TDer;
end;

{ TBase }

class function TBase.MYType: TMyType;
begin
  Result:=TMyType(Self);
end;

function TBase.Say: String;
begin
   Result:='TBase';
end;

{ TDer }

function TDer.Say2: String;
begin
  Result:='TDer';
end;

{ TMyType }

class function TMyType.AsDer: TDer;
begin
  Assert(Assigned(Self));
  Result := TDer(Self) ;
end;

Sample usage is below, it’s calls method but when set/get field’s raise error.

procedure TForm1.Button1Click(Sender: TObject);
var
  b,c:TBase;
begin
  b:=TDer.Create;
  c:=b.MYType.AsDer;

  ShowMessage(b.MYType.AsDer.Say2); // OK. Running      
  if (@b<>@c) then ShowMessage('Not Equal');  // Shows message, Why ?
  b.MYType.AsDer.a:='hey'; // Error

  FreeAndNil(b);
end;

Do you have any idea?

  • 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-06-03T18:00:39+00:00Added an answer on June 3, 2026 at 6:00 pm

    The fundamental problem is here:

    class function TBase.MYType: TMyType;
    begin
      Result:=TMyType(Self);
    end;
    

    This is a class method and so Self refers to a class and not an instance. Casting it to be an instance does not make it so. Exactly the same error is made in your AsDer class function.

    Looking into the specifics, the call to

    b.MYType.AsDer.Say2
    

    is benign and appears to work fine because it does not refer to Self. You could equally write TDer(nil).Say2 and that code would also work without problem. Now, if the function Say2 referred to Self, that is referred to an instance, then there would be a runtime error.

    @b<>@c
    

    always evaluates to true because you are comparing the locations of two distinct local variables.

    b.MYType.AsDer.a
    

    is a runtime error because AsDer does not return an instance of TDer. So when you attempt to write to a you have a runtime error. This is because you are referring to Self and that’s why this code fails, but the earlier call to Say2 does not.


    I’m not really sure what you are trying to do here, but it looks all wrong. Even if you were working with instance methods rather than class methods, it would simply be wrong to case a base class instance to a derived class instance. If something is the wrong type, no amount of casting will turn it into the right type.

    Furthermore, you should never write code that has a method of TBase assuming it is of type TDerived. The base class should know absolutely nothing of its derived classes. That is one of the very basic tenets of OOP design.

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

Sidebar

Related Questions

I am using a fictional example for this. Say, I have a Widget class
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
Want the function to sort the table by HP but if duplicate HPs then
I have extended PDOStatement and modified the fetch() method to typecast values of the
I have a function uint8_t EE_Write(uint8_t addr, uint8_t len, uint8_t * buf) that takes
The Scenario is: Object anObj=7; Object anotherObject=7.77; Class aClass=Class.forName(java.lang.Integer); Class anotherClass=Class.forName(java.lang.Double); I want to
I have a table that registers visitors to a website. I want to count
I know I shouldn't be using that function, and I don't care. Last time
So let's say I have a char and I want to strcat() it to

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.