Recent

Author Topic: [SOLVED] Enumerator in TFPGMap  (Read 1506 times)

janasoft

  • Jr. Member
  • **
  • Posts: 51
[SOLVED] Enumerator in TFPGMap
« on: December 27, 2023, 08:41:53 pm »
I try to use an enumerator in TFPGMap but I can't. I use this code that I've found in a a forum but has an error because Index and Data needs an index
Code: Pascal  [Select][+][-]
  1. uses fgl;
  2.  
  3. type
  4.   TMyDict = specialize TFPGMap<string, integer>;
  5. var
  6.   Dict: TMyDict;
  7.   Key: string;
  8.   Value: integer;
  9. begin
  10.   Dict := TMyDict.Create;
  11.   try
  12.     // Add some key-value pairs to the map
  13.     Dict.Add('One', 1);
  14.     Dict.Add('Two', 2);
  15.     Dict.Add('Three', 3);
  16.  
  17.     // Loop through the keys and print them
  18.     for Key in Dict.Keys do      // Must be Keys[index]
  19.       writeln('Key: ', Key);
  20.  
  21.     // Loop through the values and print them
  22.     for Value in Dict.Data do   // Must be Data[index]
  23.       writeln('Value: ', Value);
  24.  
  25.     // Access a value by its key
  26.     writeln('The value of "Two" is: ', Dict['Two']);
  27.  
  28.     // Remove a key-value pair from the map
  29.     Dict.Remove('Three');
  30.     writeln('The map size is now: ', Dict.Count);
  31.   finally
  32.     Dict.Free;
  33.   end;
  34. end.

If I try
Code: Pascal  [Select][+][-]
  1.  for Key in Dict do

I get the error: Cannot find an enumerator for the type "TFPGMap$2$crc3DEDFEAF"

What I'm doing wrong?

Regards
« Last Edit: December 29, 2023, 09:48:29 pm by janasoft »

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: Enumerator in TFPGMap
« Reply #1 on: December 27, 2023, 08:49:40 pm »
Hi
This:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyDict = specialize TFPGMap<string, integer>;
is essentially a new type, so I would think, you have to define an enumerator for this new type too...?!?  %)
Just my "Nickles Worth"  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

janasoft

  • Jr. Member
  • **
  • Posts: 51
Re: Enumerator in TFPGMap
« Reply #2 on: December 27, 2023, 10:57:01 pm »
Thank you for your answer.
Looking at the code I thought that TFPGMap had some kind of enumerator.
So I will try to do my own enumerator.
Regards

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Enumerator in TFPGMap
« Reply #3 on: December 28, 2023, 06:12:14 am »
AFAIK there's no enumerator defined for TFPGMap, only for TFPG[[Interfaced]Object]List. Indeed everywhere each implementation has their own convention, some return the keys, some return the data, some return a compound type having both as fields. So TFPGMap just provides the first two ways for anyone to use accordingly, as the third way can be done using good ol' classic square bracket indexing.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6398
  • Compiler Developer
Re: Enumerator in TFPGMap
« Reply #4 on: December 29, 2023, 04:21:34 pm »
If I try
Code: Pascal  [Select][+][-]
  1.  for Key in Dict do

I get the error: Cannot find an enumerator for the type "TFPGMap$2$crc3DEDFEAF"

What I'm doing wrong?

Nothing, because TFPGMap<,> does not provide an enumerator.

janasoft

  • Jr. Member
  • **
  • Posts: 51
Re: [SOLVED] Enumerator in TFPGMap
« Reply #5 on: December 29, 2023, 09:56:07 pm »
The code I inserted in my first post seemed correct and that was what led me to the mistake.
I will try to make one.
Anyway, many thanks for you answers.

 

TinyPortal © 2005-2018