Jump to content

Recommended Posts

Hi,

So here is what I want to achieve.

I have a class named TA. in class TA, I have a record named TB.

is it possible to get the constructed instance of TA as a variable in TB without the use of an external variable that is assigned at the construction of TA of course?

 

type
  TA = class abstract(TObject)
  strict private
  type
    TB = record
    private
    var
      ClassAInstance: TA;  // ========================> is it possible to get the constructed instance of TA in TB so I can assign it to the variable.
      Block: array [0 .. 7] of Byte;
      Offset: UInt64;
      BlockOffset: Int32;

      function Clone(): TB;

      procedure Read(const ADestination: TBytes;
        ADestinationOffset, AOutputLength: UInt64);

      class function DefaultB(): TB; static;

    end;

Thanks.

Share this post


Link to post

Not sure that I understand the full context, but assuming you have an instance of TB in TA, overriding TA.AfterConstruction would allow you to assign self to that instance of TB?

 

Share this post


Link to post

By itself, TB does not really have much to do with TA. It's just a type definition, it's full type name is TA.TB.

 

Since it's just a type definition and no instance, you could have multiple instances of TB inside your TA instance. You can have TA.TB instances outside of a TA instance. You can have a TA instance with no TA.TB inside at all.

  • Thanks 1

Share this post


Link to post

Not outside, as it's strict private. You could pass TA instances to the method you are using to initialize your record.

 

class function DefaultB(ATA: TA): TB; static;
begin
 Result.ClassAInstance := ATA;
end;

 

  • Like 1

Share this post


Link to post
35 minutes ago, Lars Fosdal said:

Not sure that I understand the full context, but assuming you have an instance of TB in TA, overriding TA.AfterConstruction would allow you to assign self to that instance of TB?

 

Hmm, That looks like something that could work.

will check it out, Thanks.

  • Like 1

Share this post


Link to post
13 minutes ago, Attila Kovacs said:

Not outside, as it's strict private. You could pass TA instances to the method you are using to initialize your record.

 


class function DefaultB(ATA: TA): TB; static;
begin
 Result.ClassAInstance := ATA;
end;

 

Don't know why I didn't think of this in the first place.

Thanks a lot.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×