Solved

implicit typecasting

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now