?
Solved

Reference parent property of custom type

Posted on 2006-10-26
7
Medium Priority
?
176 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
5 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 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