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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:31:15+00:00 2026-05-18T08:31:15+00:00

Alright, so I have a base class which we’ll call TFruit . From this

  • 0

Alright, so I have a base class which we’ll call TFruit. From this there are various descendants like TApple, TOrange and so on. I need to save the properties of the descendant classes to a file.

In order to be able to create the right class when loading the data, each class needs to have an ID that I write to the file before writing the actual data. Currently, I’ve come up with the following way of doing it:

type
  TFruit = class
    const ID = 0;
  end;

  TApple = class(TFruit)
    const ID = 1;
  end;

  TOrange = class(TFruit)
    const ID = 2;
  end;

Testing this, I found out that I need to be super careful which class I declare. If I use this:

  var Fruit: TFruit;

  Fruit := TOrange.Create;

…then Fruit.ID will return zero. However, declaring Fruit as a TOrange will yield the expected result Fruit.ID = 2 (anyone know why?)

So basically, am I doing this right or is there a better way to do it? Having to create a class function and return a value from there seems very ugly by comparison (extra function declaration, implementation and code).

  • 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-18T08:31:15+00:00Added an answer on May 18, 2026 at 8:31 am

    An easier to maintain solution would be to create a mapping class where you register all classes you’d like to convert to an integer.

    Advantages

    • Ability to detect duplicate registrations.
    • Independent of your class structure.
    • Includes the transformation back to a classname.

    Usage

      RegisterClass.Register(0, TFruit);
      RegisterClass.Register(1, TApple);
      RegisterClass.Register(2, TOrange);
    

    Implementation

      TRegisterClass = class
      private
        FList: TStringList;
      public
        function FindID(AClass: TClass): Integer;
        function FindClassName(const ID: Integer): string;
        procedure Register(const ID: Integer; AClass: TClass);
      end;
      ...
      function TRegisterClass.FindID(AClass: TClass): Integer;
      begin
        Assert(Assigned(AClass));
    
        Result := -1;
        if FList.IndexOf(AClass.ClassName) <> -1 then
          Result := Integer(FList.Objects[FList.IndexOf(AClass.ClassName)]);
      end;
    
      function TRegisterClass.FindClassName(const ID: Integer): string;
      var
        I: Integer;
      begin
        Result := EmptyStr;
        for I := 0 to Pred(FList.Count) do
          if Integer(FList.Objects[I]) = ID then
          begin
            Result := FList[I];
            Exit;
          end;
      end;
    
      procedure TRegisterClass.Register(const ID: Integer; AClass: TClass);
      begin
        if IsAlreadyRegistered(ID) then 
          raise Exception.Create('Duplicate ID Registration')
        else if IsAlreadyRegistered(AClass) then 
          raise Exception.Create('Duplicate Class Registration');
    
        FList.AddObject(AClass.ClassName, Pointer(ID)); 
      end;
    

    Please note that there are better structures to map a String to an Integer. Writing this without a compiler and don’t knowing many basic structures beyond Delphi5, I’ve chosen an obvious implementation.

    Note that the IsAlreadyRegistered overloaded functions still have to be written

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

Sidebar

Related Questions

Alright so I have this C++ image capturing class. I was wondering if I
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright, let's say I have a table that looks like this: ID | DATE
I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many
Alright, I have a div which is 50px (height), and the width does not
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
Alright im doing a site which is available in multiple languages. I have ran
Alright I have been trying to figure this out and I read the MSDN
Alright I have the MYSQL query code setup to pull this information out of

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.