Recent

Author Topic: [Solved] Enumeration of A TCollection descendant with FOR .. IN  (Read 3484 times)

@nton

  • New Member
  • *
  • Posts: 10
Hi,

I try to enumerate a TCollection descendant with the for .. in statement. The following error occurs:

Quote
main.pas(172,10) Error: Incompatible types: got "TCollectionItem" expected "TWBBusinessComponent"

Code: [Select]
var
  ABusComp: TWBBusinessComponent;

//
    for ABusComp in BusinessComponents do  { Error here! }
    begin   

    end

//


Where BusinessComponents is a TWBBusinessComponentCollection with TWBBusinessComponent as items

With the folowing definitions:

Code: [Select]

TWBBusinessComponentCollection = class(TCollection)

TWBBusinessComponent = class(TCollectionItem)


Should I make some special enhancements to the TCollectionItem descendant to make this work?
« Last Edit: May 08, 2014, 02:24:50 pm by @nton »
Lazarus 1.26 32 bit, FPC 2.64, Windows 7, ZEOS 7.1.4

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Enumeration of A TCollection descendant with FOR .. IN
« Reply #1 on: May 08, 2014, 01:16:09 pm »
I'm afraid it is impossible this way. For .. in works for strings, sets or generics but TCollection.Items is TFPList.
You will have to use:
Code: [Select]
for i:=0 to Collection.Count-1 do
  with Collection.Items[i] do
    ...
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Enumeration of A TCollection descendant with FOR .. IN
« Reply #2 on: May 08, 2014, 01:23:26 pm »
You should change your TWBBusinessComponentCollection. Try to declare the aBusComp as a TCollectionItem and see if that works, if it does, which I presume that it will, then you need to write an enumerator for TWBBussinessComponent and a method in TWBBusinessComponentCollection that will return that enumerator. For an example just take a look on Tcollection.GetEnumerator and TCollectionEnumerator implementation in the classes unit.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Enumeration of A TCollection descendant with FOR .. IN
« Reply #3 on: May 08, 2014, 01:33:09 pm »
So for..in loop is more powerful than I expected.
With
Code: [Select]
aBusComp: TCollectionItem; it works. You will only need the casting:
Code: [Select]
for ABusComp in BusinessComponents do 
  with ABusComp as TWBBusinessComponent do
    begin   

    end
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

@nton

  • New Member
  • *
  • Posts: 10
Re: Enumeration of A TCollection descendant with FOR .. IN
« Reply #4 on: May 08, 2014, 02:24:18 pm »
Thanks!

It works indeed!

To lose the typecast I should probably override the implementation of IEnumerator and IEnumerable on the TCollection descendant.
(A little too much work for now ...)

Lazarus 1.26 32 bit, FPC 2.64, Windows 7, ZEOS 7.1.4

 

TinyPortal © 2005-2018