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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:40:48+00:00 2026-05-27T08:40:48+00:00

Original Question I have this situation: type X1 = Class protected oMyList: TMyList; public

  • 0

Original Question

I have this situation:

type
  X1 = Class
  protected
    oMyList: TMyList; 
  public
    property MyList: TMyList write oMyList;
  end;

  X2 = Class(X1)
  public
    procedure GetMyList;
  end;

with:

  procedure X2.GetMyList;
  begin
    Writeln (oMyList.Count);    // <-- Return exception
  end;

and in main program:

var
  P: X2;
  MyList: TMyList;
begin
  P := X2.Create;
  try
    P.MyList := MyList;
    P.GetMyList;
  finally
    P.Free;
  end;
end;

The problem is an exception when I try to read oMyList.Count. Of course, MyList is created and defined correctly.

Where is my mistake?

Updated Question

I have this situation:

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.Generics.Collections;

// Module 1

type
  PDBEstr = Integer;  // Just an example of list with integer
  TDBEstr = TList<PDBEstr>;

  TArchive = class
  protected
    oDBEstr: TDBEstr;
  public
    constructor Create;
    destructor Free;
    property DBEstr: TDBEstr read oDBEstr;
  end;

constructor TArchive.Create;
begin
  oDBEstr := TList<PDBEstr>.Create;
  oDBEstr.Add(36);  // Add an element to list
end;

destructor TArchive.Free;
begin
  oDBEstr.Free;
end;

// Module 2

type
  TX0 = class
  protected
    oArchive: TArchive;
    function GetDBEstr: TDBEstr;
  public
    constructor Create;
    destructor Free;
    property DBEstr: TDBEstr read GetDBEstr;
  end;

constructor TX0.Create;
begin
  oArchive := TArchive.Create;
end;

destructor TX0.Free;
begin
  oArchive.Free;
end;

function TX0.GetDBEstr: TDBEstr;
begin
  Result := oArchive.DBEstr;
end;

// Module 3

type
  TX1 = class
  var
    oDBEstr: TDBEstr;
  public
    property DBEstr: TDBEstr read oDBEstr write oDBEstr;
    procedure Load;
  end;

procedure TX1.Load;
begin
  writeln (oDBEstr.Count);  // Return 1
end;

// Module 4

type
  TX2 = class(TX1)
  public
    constructor Create;
  end;

constructor TX2.Create;
begin
  inherited;
  // In this point i need to have access to oDBEstr for work with data
  // in oDBEstr
  writeln(oDBEstr.Count);   // <----- Return Exception
end;

// Main Program

var
  X0: TX0;
  X2: TX2;
begin
  try
    X0 := TX0.Create;
    try
      writeln(X0.DBEstr.Count);  // Return 1
      writeln(X0.DBEstr.First);  // Return 36
      X2 := TX2.Create;
      try
        X2.DBEstr := X0.DBEstr;
        writeln(X2.DBEstr.count);  // Return 1
      finally
        X2.Free;
      end;
    finally
      X0.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

All work perfectly, only in TX2.Create I have exception when I try to read oDBEstr. Of course, I take as example TX2.Create, but I need to have access to oDBEstr in all TX2, not only in TX2.Create.

  • 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-27T08:40:49+00:00Added an answer on May 27, 2026 at 8:40 am

    Note: This answer refers to the updated question.

    In your updated code you simply do not create oDBEstr before you access it. The constructor of TX2.Create does not create it, and the class that TX2 is derived from does not have a constructor. So naturally an access violation occurs.

    The intent of these classes appears to be that TX1 and hence TX2 hold a reference to a DBEstr that is owned by a different class. You have implemented a read/write property that allows users of TX1 and TX2 to set this reference. However, they can’t do so until the constructor has finished running. The users of TX1 and TX2 can only set that property once they have a reference to a TX1 or TX2. So, to solve the conundrum your only option is to pass the DBEstr instance to the constructor of TXE2 as a parameter.

    Here’s a working version of your code this illustrates how to do this:

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils, System.Generics.Collections;
    
    // Module 1
    
    type
      PDBEstr = Integer;  // Just an example of list with integer
      TDBEstr = TList<PDBEstr>;
    
      TArchive = class
      protected
        oDBEstr: TDBEstr;
      public
        constructor Create;
        destructor Destroy; override;
        property DBEstr: TDBEstr read oDBEstr;
      end;
    
    constructor TArchive.Create;
    begin
      oDBEstr := TList<PDBEstr>.Create;
      oDBEstr.Add(36);  // Add an element to list
    end;
    
    destructor TArchive.Destroy;
    begin
      oDBEstr.Free;
      inherited;
    end;
    
    // Module 2
    
    type
      TX0 = class
      protected
        oArchive: TArchive;
        function GetDBEstr: TDBEstr;
      public
        constructor Create;
        destructor Destroy; override;
        property DBEstr: TDBEstr read GetDBEstr;
      end;
    
    constructor TX0.Create;
    begin
      oArchive := TArchive.Create;
    end;
    
    destructor TX0.Destroy;
    begin
      oArchive.Free;
      inherited;
    end;
    
    function TX0.GetDBEstr: TDBEstr;
    begin
      Result := oArchive.DBEstr;
    end;
    
    // Module 3
    
    type
      TX1 = class
      var
        oDBEstr: TDBEstr;
      public
        property DBEstr: TDBEstr read oDBEstr write oDBEstr;
        procedure Load;
      end;
    
    procedure TX1.Load;
    begin
      writeln (oDBEstr.Count);  // Return 1
    end;
    
    // Module 4
    
    type
      TX2 = class(TX1)
      public
        constructor Create(ADBEstr: TDBEstr);
      end;
    
    constructor TX2.Create(ADBEstr: TDBEstr);
    begin
      inherited Create;
      oDBEstr := ADBEStr;
      writeln(oDBEstr.Count);
    end;
    
    // Main Program
    
    var
      X0: TX0;
      X2: TX2;
    begin
      try
        X0 := TX0.Create;
        try
          writeln(X0.DBEstr.Count);  // Return 1
          writeln(X0.DBEstr.First);  // Return 36
          X2 := TX2.Create(X0.DBEstr);
          try
            writeln(X2.DBEstr.count);  // Return 1
          finally
            X2.Free;
          end;
        finally
          X0.Free;
        end;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
      Readln;
    end.
    

    I realise that this code is an illustrative example culled from your real code but I question the use of a read/write property for TX1.DBEstr. I suspect that each instance of TX1 will use the same DBEstr instance for the entire lifetime of the TX1 instance. If so then it should be passed in to a constructor on TX1 and the property made read-only:

    type
      TX1 = class
      var
        oDBEstr: TDBEstr;
      public
        constructor Create(ADBEstr: TDBEstr);
        property DBEstr: TDBEstr read oDBEstr;
        procedure Load;
      end;
    
    constructor TX1.Create(ADBEstr: TDBEstr);
    begin
      inherited Create;
      oDBEstr := ADBEStr;
    end;
    

    This change would lead to the following implementation for TX2

    type
      TX2 = class(TX1)
      public
        constructor Create(ADBEstr: TDBEstr);
      end;
    
    constructor TX2.Create(ADBEstr: TDBEstr);
    begin
      inherited;
      writeln(oDBEstr.Count);
    end;
    

    Note as an aside that I have fixed the destructors in your code. Destructors should always be called Destroy, they should always be declared with the override keyword, and the should always call inherited as their final action. Note that you never call Destroy directly, rather you call Free. This convention is to enable Free to be a null operation on an uninitialized nil object reference which is a great convenience when coding destructors.

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

Sidebar

Related Questions

I have a class, called ClassX for the purposes of this question, declared as
Let's say I have this class and its subclass @interface MySuperClass - (void)open:(id)type value:(id)value;
I have this situation (it is very simplified for the sake of this question):
Edit: original question below, but I revise it now that I have some code
This question is sort of a follow-up to my original question here . Let's
This is a continuation of this question: Original Question (SO) The answer to this
UPDATE: This question is out of date, but left for informational purposes. Original Question
I am reposting this question due to inability to solve the problem (original here
I am aware of this question , but the original poster accepted a solution
I have this situation that I need to let users define decisions based on

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.