Solved

Reference parent property of custom type

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

14 Experts available now in Live!

Get 1:1 Help Now