?
Solved

Reference parent property of custom type

Posted on 2006-10-26
7
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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