Solved

Reference parent property of custom type

Posted on 2006-10-26
7
168 Views
Last Modified: 2013-11-23
This is a cut down version of the sort of thing I have

MyType = ( item1, item2, item3);

TMyObject = class( TObject)
  private
    MyType: TMyType;  // I want to remove this property

    function GetMyType: TMyType;
  public
    property MyType: TMyType read GetMyType;
  end;

TMyObjectList = class( TObjectList)
  private
    MyType: TMyType;  // i want to be able to reference this parent property from the objects in the objectlist
  end;


What I don't like is the fact that I have the property MyType in both the parent and child.
I would rather just keep it in the parent (objectlist), anmd be able to reference it from the child

Obviously I know this code below will not compile, but hopefully someone gets the idea of what I want to do.
I would assume that this might normally be done using a "class property", but they don't exist in delphi.

function TMyObject.GetMyType: TMyType;
begin
  Result := Parent.MyType
end;
0
Comment
Question by:mikelittlewood
  • 3
  • 2
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 17810258
?? what is the parent???
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 17810285
The TMyObjectList is the parent holding n number of TMyObject's
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 17810291
Currently both the TMyObjectList AND each TMyObject have a property MyType, but I want to remove the property from the child TMyObject and reference the property MyType in the parent TMyObjectList from within the child.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 17810320
maybe this way??

TMyObjectList = class( TObjectList)
  published
    property MyType: TMyType[Index] read GetMyType;  //not sure about the syntax here (just from head)
  end;


function TMyObjectList.GetMyType(index : integer) : TMyType
begin
  //check bounds
  if (index < 0) or (index > count) then
   raise exception.create('Index out of bounds');
  result := TMyObject(items[index]).MyType;
end;

just from head

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 250 total points
ID: 17810333
>TMyObject and reference the property MyType in the parent TMyObjectList from within the child
oops the other direction, thinking . . .

you need an attribute in your TMyObject, which references the objectlist where it is added into.
you may keep in mind that it could be added into multiple lists
-> so the TMyObject should add/remove itself

meikl ;-)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question