Question about sorting a TObjectList

Hello,
In order to sort a TObjectList I wrote the function below.
The list can have items of different types but it can only be sorted if all items are TItem.
The problem is that I need to know the type of items and raise an exception during sort process if some of them are not TItem.
The code below will not compile. Any idea?
Thanks.
(Using D2009)
{code}
function CompareByName(item1, item2 : Pointer): Integer;
var
   I1 : TItem;
   I2 : TItem;
begin
   if not (item1 is TItem) or not (item2 is TItem) then
      raise EErrorInList.Create('Invalid list.');
   ...
   I1 := TItem(item1);
   I2 := TItem(item2);
   ...
end;
{code}
0
Jose
3/5/2010 11:02:59 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

2 Replies
346 Views

Similar Articles

[PageSpeed] 36

> {quote:title=Jose Alex Miranda wrote:}{quote}
> Hello,
> In order to sort a TObjectList I wrote the function below.
> The list can have items of different types but it can only be sorted if all items are TItem.
> The problem is that I need to know the type of items and raise an exception during sort process if some of them are not TItem.
> The code below will not compile. Any idea?
> Thanks.
> (Using D2009)
> {code}
> function CompareByName(item1, item2 : Pointer): Integer;
> var
>    I1 : TItem;
>    I2 : TItem;
> begin
>    if not (item1 is TItem) or not (item2 is TItem) then
>       raise EErrorInList.Create('Invalid list.');
>    ...
>    I1 := TItem(item1);
>    I2 := TItem(item2);
>    ...
> end;
> {code}
As long as they all derive from tobject (ie there's no typecasted integers etc)
if not ( tobject(item1) is TItem) or not ( tobject(item2) is TItem) then
 raise EErrorInList.Create('Invalid list.');

or
you could create a descendant of tobjectlist that keeps count of how many non-titems are added/removed to/from the list
--
Any code shown is just an example, take a look, then go and read the docs
0
karl
3/5/2010 11:35:16 PM
Jose Alex Miranda wrote:

> Hello,
> In order to sort a TObjectList I wrote the function below.
> The list can have items of different types but it can only be sorted
> if all items are TItem.  The problem is that I need to know the type
> of items and raise an exception during sort process if some of them
> are not TItem.  The code below will not compile. Any idea?  Thanks.
> (Using D2009)
> {code}
> function CompareByName(item1, item2 : Pointer): Integer;
> var
>    I1 : TItem;
>    I2 : TItem;
> begin
>    if not (item1 is TItem) or not (item2 is TItem) then
>       raise EErrorInList.Create('Invalid list.');
>    ...
>    I1 := TItem(item1);
>    I2 := TItem(item2);
>    ...
> end;
> {code}

You have to change "(item1 is TItem)" into "(TObject(item1) is TItem)".
-- 
Uwe
0
Uwe
3/5/2010 11:39:27 PM
Reply:

Similar Artilces:

General bootstrap questions: Delphi 7, pascal
Hi, This might be a bit inchoherant. I am sorry of that is the case but I have inherited a collection of languishing Delphi Projects in Pascal. I do not know Pascal and this is nearly my into to Delphi. Delphi 7 is running in a Windows XP VirtualMachine instance. The project in question uses several third party libraries / tools. Basically, I don't know where to start so I'll start at the beginning and hope you guys can help. I first inherited these projects almost two years ago. For the most part they work. There is one project, however, that needs to have a few "mag...

A general question about supporting offline databases
I am a bit of a newbie with Delphi databases, we've been using Delphi for 15 years but we have always used a proprietary binary data format. We now want to switch to an SQL file format. For now we're working with SQLITE, so the Java and C# guys in our shop can read the file too. We have all the latest tools now, XE5 with FireDAC, Datasnap, REST etc. I am very, very impressed with what we can do now. Currently, we have a Delphi desktop app that controls the data file, and use XMLRPC calls from the clients to send and receive transactions. We have what we call a "refresh&...

General question on the concept of integer handling in Delphi 2009
I found in system.pas new names "NativeInt" and "NativeUInt". According to all computer languages worldwide, Integer (and Cardinal) is *already* a native integer that represents the CPU register for best performance etc... What will happen in Delphi 2010 Win64 ??? I don't want to change Integer to NativeInt in my code thousand times to get adequate performance. > {quote:title=Ralf Stocker wrote:}{quote} > I found in system.pas new names "NativeInt" and "NativeUInt". Interesting. > According to all computer languages wor...

Where's tList<T>.sorted ? (Delphi 2009+ question)
Hi Is there anyway to find out whether a generic tList is sorted? It's possible to sort generic tLists, using the Sort method, but there's no Sorted property to find out if the list is sorted or not. Regards Simon simon horup wrote: > It's possible to sort generic tLists, using the Sort method, but > there's no Sorted property to find out if the list is sorted or not. Even on a regular TList, Sorted does not indicate with certainty whether the list is sorted. Rather, it indicates whether the list will be REsorted when it is changed. A list can be...

Delphi and Delphi Prism licensing question
When I buy Delphi and Delphi Prism, do I get a "named user licence" or a "workstation licence"? Can I install both products on two machines I use with the same licence keys? I have a Mac Pro ("Cyrus") running Vista 64 in VMware and a MacBook ("Blacky") running Vista 32 in Parallels. Can I install Delphi and Delphi Prism on both Cyrus and Blacky since I use both machines (and like to have all dev tools I use installed on both machines at all times)? Andrew Brehm wrote: > When I buy Delphi and Delphi Prism, do I get a "named user licence...

Question about Delphi 2009 vs Delphi 7
Hi, I was wondering if any of you can point me to a URLs that show the most important improvements between these versions of Delphi. The objective is convincing the powers-that-pay-the-bills that moving to D2009 (that we'd have to purchase) is a better idea than moving to D7 (for which we have licenses). Fire at will! -- Saluti, Alessandro Federici ------------------------------- "I have been through some terrible things in my life, some of which actually happened." (Mark Twain) Alessandro Federici wrote: > Hi, > I was wondering if any of yo...

automate form question and general http questions
Dear all, There are lots questions. I appologize at first. I am trying to automate a form submission. in html the form has <select name=p2 size=1 onChange="showimage()"><option value="01">123</option> <input type="radio" value="" checked name="show">001 <input type="radio" value="0" name="show">001 i am wondering can i do it as $ua->post($url,[p2=>'01',show=>'0']); which corresponds to choose option '123' and radio '001'? or...

anim_parrot_logo.imc: .include question + general imcc questions
Hello, While looking at Chromatic's anim_parrot_logo.imc (in examples/sdl), I was wondering why the includes weren't at the same place. Indeed, the source reads: ..sub _main _init() _MAIN() end ..end ..include "library/sdl_types.imc" ..pcc_sub _init prototyped .include "library/sdl.pasm" .pcc_begin_return .pcc_end_return ..end Why does: ..sub _main _init() _MAIN() end ..end ..include "library/sdl_types.imc" ..include "library/sdl.pasm" fails with: error:imcc:parse error, unexpec...

windows ME questions, questions, questions.......
Hi, my next PC will be running Windows ME. The PC will be supplied via my work, so there's no choice here for me (ME?) (I think I would have preferred 98 SE). The harddisk (40GB matrox) will have ME installed, and both the Windows ME set-up files and an image of the initial Harddisk 'on a hidden partition'. Word has it that this partition is not seen by Format nor FDisk. Is this really possible? No Windows CD will be supplied. Seems a new way of MS to encourage working with licensed software only. Anyone familiar with this way of distributing an OS? Will I be able to ...

General Questions
hi all I am having trouble finding answer for the following questions if anyone knows any answers from these questions please do reply as soon as possible! thanks 1. What is the PFC Application Manager and how is used in PFC applications? And, describe the details of the relationship of the PowerBuilder Application Object to the PFC Application Manager. 2. Describe the steps required to create a MDI application using PFC from scratch in sequence and where appropriate indicate why the sequence is necessary. (At least, one complete sentence per step.) Where and how c...

General questions
2 questions I have created a datawindow which is quite wide, the user has to scroll left and right to see the entire report, does anyone know of a property or technique which would allow the user to lock the titles on the left side of the report I need to suppress a line in a header on all but the first page, I have set the visible property to 0 if page <> 1 but, I need the header band to shrink as to free up the space left by the invisible header. Is there a way to do this?? /Wayne For #1: Set the HSplitScroll attribute of the DW control to true. For #2: Unfortuna...

Question about questions
Name: Edward Newill Email: ernewillatyahoodotcom Product: Thunderbird Summary: Question about questions Comments: Is there an email address that I can send a question too? I could not find one in the Service area of your site. I would like to know how I create signatures for my Thunderbird email? Best regards, E. Newill Browser Details: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727) From URL: http://hendrix.mozilla.org/ ...

General Questions..
Hello to All the moderators / Trusted advisor and everyone ! I am new born kid of this .NET block so as usual have to ask lots of things. I have good understanding of ASP and now want to migrate from ASP to ASP.NET. Would someone kind enough to clear my doubts.. 1) What is ASP.NET, VB.NET and C # ? This is my understanding so far about the above question.. In dot net, one need to use any of these programming language like C #, VB.NET. As like one use VBScript in ASP. I want more details... 2) How a new born baby should proceed in ASP.NET ?( Most important one) 3) What are...

Few questions about Delphi...
Can legacy Delphi code (Delphi 4, 5 etc) be developed using Delphi 2005? Which is the latest version of Delphi that supports legacy code? Thanks! <Karthik Kotha> wrote in message news:302424@forums.embarcadero.com... > Can legacy Delphi code (Delphi 4, 5 etc) be developed using Delphi 2005? > Which is the latest version of Delphi that supports legacy code? > > What do you mean by legacy code? All versions of Delphi compile the same old Object Pascal language. You can compile D4 code in Delphi 2007 with almost no change. With pro 2009 versions the only c...

Web resources about - Question about sorting a TObjectList - embarcadero.delphi.general

Resources last updated: 11/23/2015 8:28:28 AM