Solved

implicit typecasting

Posted on 2001-06-22
4
443 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
[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
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

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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