Solved

Reference parent property of custom type

Posted on 2006-10-26
7
167 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

947 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now