Solved

implicit typecasting

Posted on 2001-06-22
4
439 Views
Last Modified: 2010-04-06
Hi

I have created a new type TMyType. Now I want to have a random number of this type in a list, array, etc.

I first used a TObjectList, which worked fine, but I got some ugly code, because I had to typecast to my type the whole time.
Like this (ObjectList[i] as TMyType).SomeProperty

I could use an array to make the code a bit nicer, but as I add and remove objects an array won't do.

Is it possible to create an ObjectList that works with TMyType.
It would be perfect if something like templates from C++ were around.
0
Comment
Question by:bnz
4 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 6217458
You can derive your own TMyTypeList from TList to hold TMyType type elements just as TObjectList does for TObject type elements...
0
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
ID: 6217872
Hi bnz,

here is a short sample how to create list to keep your objects

  // your object
   TMyType = class(TObject)
     Name: String;
   ...
   end;

  // list that keeps your objects
  TMyTypes = class(TObjectList)
  private
      function GetItem(Index: Integer): TMyType;
  public
     property Items[Index: Integer] read GetItem; defsult;
  end;

implementation

function TMyTypes.GetItem(Index: Integer): TMyType;
begin
  Result := TMyType(Inherited Items[Index]);
end;

......
var
  MList: TMyTypes;
......
  MList[10].Name := 'Some name';
 
-------
Igor.  
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6220383
Another way to do implicit typecasts instead of explicit ones it to use a normal TList and to have a temporary variable of TMyType. Since TList deals with generic pointers, you can assign them to and from a TMyType variable without typecast, thus making Delphi do an invisible typecast.
0
 
LVL 4

Expert Comment

by:DMN
ID: 6220463
Also it's possible to use TCollection and TCollectionItem
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…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

28 Experts available now in Live!

Get 1:1 Help Now