Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

implicit typecasting

Posted on 2001-06-22
4
Medium Priority
?
446 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 300 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 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