Recent

Author Topic: vb6 Collection Equivalent  (Read 2334 times)

axisdj

  • Jr. Member
  • **
  • Posts: 64
vb6 Collection Equivalent
« on: April 19, 2019, 12:16:02 am »
Hello ,

Moving from VB6, and one pattern I use alot is creating a collection of objects. In vb6 the Object type does not need to be defined and I can store objects in a collection with keys, and then access those by casting the collection item to the object type.

rough example

''decleration
class myClass
 public var1 as string
 public var2 as string
end class

function someFunction

dim aClass as new MyClass
''populate aClass

dim myCollection as new collection
myCollection.add(aClass,'key')


later...

function anotherFunction()
dim aClass as myClass
set aClass = myCollection(x)


Is there such a thing in Lazarus, or do all tList/TCollection require type decleration/inheritance?

Thanks
« Last Edit: April 19, 2019, 12:22:43 am by axisdj »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: vb6 Collection Equivalent
« Reply #1 on: April 19, 2019, 12:25:54 am »
Lookup the TStringList class: it can contain strings, in the form "key=value" if you want/need it, and and asocciated object for each string.

Fact is most list (in the wide meaning of "list") classes have similar capabilities.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: vb6 Collection Equivalent
« Reply #2 on: April 19, 2019, 01:24:55 am »
Tlist will do as you want because it stores objects and each object has a NAME:String that you can define if you wish...

 Dynamically creating objects do not get this name field filled so you can use this..

 I know a TStringlist was suggested here but it does not have to go that far..

 I've done this myself and it seems to work..

 I know VB can do a lot of things but Object PAscal/Delphi has pretty much covered this very well.
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: vb6 Collection Equivalent
« Reply #3 on: April 19, 2019, 01:36:33 am »
Tlist will do as you want because it stores objects and each object has a NAME:String that you can define if you wish...

That's not TList: TList just manages a list of pointers. What you meant, I think, is TCollection which is used for collections of named objects, with each item having a DisplayName. The drawback, for the OP's use, is that the contained items must descend from TCollectionItem

Or were you thinking of something other than Classes.TList?


ETA: Of course, there are also the specialized TClassList. TObjectList, TFPOjectList, etc. but none of them allows you to associate a string with an object with the easy of a TStringList.

I mean, you just do:
Code: Pascal  [Select][+][-]
  1. {Add an object}
  2. StringList.AddObject('key', AnObject)
  3.  
  4. { Retrieve it: }
  5. i := StringList.IndexOf('key'); { this two lines to get the index}
  6. if i >= 0 then                        { from the associated sring}
  7.   MyObject := StringList.Objects[i];
  8.  
which is basically what the OP wanted.
« Last Edit: April 19, 2019, 02:00:14 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: vb6 Collection Equivalent
« Reply #4 on: April 19, 2019, 01:48:50 am »
Sorry about that I was thinking about the MethodName of passing the  Method object to it..

 And the Tcomponent is the one with the NAME in it.



The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: vb6 Collection Equivalent
« Reply #5 on: April 19, 2019, 02:04:08 am »
You were thinking of TComponentList, then. Problem is that its items must descend from TComponent, which is a no-no for the OP's requirements.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dsiders

  • Hero Member
  • *****
  • Posts: 1052
Re: vb6 Collection Equivalent
« Reply #6 on: April 19, 2019, 03:20:16 am »
Perhaps TLookupStringList in lazutils/lookupstringlist.pas is closer to what he's looking for.
Preview Lazarus 3.99 documentation at: https://dsiders.gitlab.io/lazdocsnext

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: vb6 Collection Equivalent
« Reply #7 on: April 19, 2019, 06:33:35 am »
generic TList<TObject> or TObjectList<Tobject>? from generics.collections.
Or generic TFPGList<TObject> from fcl.
Those come close to a VB "collection" much more so than TCollection.
You could also use one of the generic maps, like generic TFPGMapObject<TKey; TData: TObject> from fgl or   TObjectDictionary<TKey, TValue> from generics.collections.
The last two are probably the best candidates. (A map is the same as a dictionary, which is the same as a collection in this context) Both have options for lifetime management as well.
If you are using 3.2.0 use rtl-generics (generics.collections) if you are using 3.0.4. either install rtl-generics (OPM) or use fcl. If you need examples, just ask.
« Last Edit: April 19, 2019, 07:20:40 am by Thaddy »
Specialize a type, not a var.

zoltanleo

  • Sr. Member
  • ****
  • Posts: 486
Re: vb6 Collection Equivalent
« Reply #8 on: April 19, 2019, 07:17:24 am »
generic TList<TObject> or TObjectList<Tobject>? from generics.collections.
Or generic TFPGList<TObject> from fcl.
+1
see here http://wiki.lazarus.freepascal.org/Generics
Win10 LTSC x64/Deb 11 amd64(gtk2/qt5)/Darwin Cocoa (Monterey):
Lazarus x32/x64 2.3(trunk); FPC 3.3.1 (trunk), FireBird 3.0.10; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: vb6 Collection Equivalent
« Reply #9 on: April 19, 2019, 09:10:53 am »
Simple examples:
1. using generics.collections:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses generics.collections;
  3. type
  4.   { Here it takes any class, but you can be more precise of course }
  5.   TMyObjectCollection = specialize TObjectDictionary<string, Tobject>;
  6.  
  7. var
  8.   C:TMyObjectCollection;  
  9. begin
  10.   { make the collection the owner of the objects }
  11.   C := TMyObjectCollection.Create([doOwnsValues]);
  12.   try
  13.     { do something with your collection }
  14.   finally
  15.     C.Free;
  16.   end;
  17. end.

2. using fgl:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses fgl;
  3. type
  4.   { Here it takes any class, but you can be more precise of course }
  5.   TMyObjectCollection = specialize TFPGMapObject<string, Tobject>;
  6.  
  7. var
  8.   C:TMyObjectCollection;  
  9. begin
  10.   { make the collection the owner of the objects }
  11.   C := TMyObjectCollection.Create(true);
  12.   try
  13.     { do something with your collection }
  14.   finally
  15.     C.Free;
  16.   end;
  17. end.

You can see they are broadly similar, also in functionality.
generics.collections has also a TFastObjectHashMap for when speed is important (has slightly less functionality, though)
Note that generics.collections is largely  compatible with Delphi's generics.collections if that may be of importance.
« Last Edit: April 19, 2019, 10:47:22 am by Thaddy »
Specialize a type, not a var.

axisdj

  • Jr. Member
  • **
  • Posts: 64
Re: vb6 Collection Equivalent
« Reply #10 on: April 19, 2019, 03:58:07 pm »
Thanks for all the replies, I will research each approach and decide which one to use.. I am definitely excited about this new endeavor I have put off for 10 years, but this community seems helpful and responsive so I feel re-assured I will make it through my 2 project 200k LOC conversions.


 

TinyPortal © 2005-2018