Click to See Complete Forum and Search --> : Array or collection
vchapran
August 8th, 2001, 07:23 AM
I need to share data received from ADO.Command object as a recordset, containing several customers records between several programs. Each record contains several values, like CustomerID, CustomerName and so on. I'm building dll to work with that data. What is the best approach to store data internally in dll - arrays for each field or collection of objects (never worked with collections)? If the second one is better, could you please provide some sample.
Thank you.
Vlad
makai
August 8th, 2001, 10:23 PM
there is so much overhead and non-performance in collections (beecause they take any object) that I write my own collection classes
you just need to write one and it is cut and paste from there on
vchapran
August 9th, 2001, 07:23 AM
Although your post doesn't help anyhow, thank you anyway.
Vlad
Clearcode
August 9th, 2001, 08:12 AM
A very basic overvies is -
Arrays are faster and more memory efficient but more prone to bugs and not as powerful,
Collections are slower but provide automatic resizing and the For Each..Next syntax to make them more powerful - plus, key indexing.
I use collections because processor time and memory is very cheap and developer time isn't.
HTH,
Duncan
-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.
Ghost308
August 9th, 2001, 08:32 AM
I would have to agree with Clearcode in that Collections are the better way to go for their safety and ease of use. You mentioned that you've had little experience with them so here is some sample code to get you started, hope it makes sense and helps!
set mycoll = new Collection 'create the collection
mycoll.Add "Element 1" 'add a few items to it
mycoll.Add "Element 2"
mycoll.Add "Element 3"
dim str
for Each str In mycoll
strmsg = strmsg & " " & str 'this just makes a string containing all the elements
next
MsgBox strmsg 'show me da money
For loops can also be used as opposed to the For Each loop I used, you would just have to let the loop go from 0 to mycoll.Count. Good luck!
Jeff
Kdev
August 9th, 2001, 10:55 AM
Another nice thing with collections is the fact that you can add data to each element AND a key if you so desire. This may not be the proper method for what I do but it worked nicely in my scenario. You can easily test to see if an item is in a collection if you are using keys by attempting to add an item to the collection with the same key. Another good point of keys is that you can remove an item from the collection without searching for it by iterating over the collection by referencing the key.
private sub Command1_Click()
Dim myColl as new Collection
Dim bFound as Boolean
on error GoTo ErrH
'Add some elements with their associated keys
myColl.Add "1", "Test1"
myColl.Add "2", "Test2"
myColl.Add "4", "Test4"
'Test if key "Test3" exists
bFound = false
myColl.Add "Temp", "Test3"
If bFound then
MsgBox myColl.Item("Test3")
else
myColl.Remove "Test3"
MsgBox "That key is not in the collection"
End If
'Test if key "Test4" exists
bFound = false
myColl.Add "Temp", "Test4"
If bFound then
MsgBox myColl.Item("Test4")
else
myColl.Remove "Test4"
MsgBox "That key is not in the collection"
End If
Exit Sub
ErrH:
If Err.Number = 457 then 'This is the error that says that the given key already exists
Err.Clear
bFound = true
resume next
else
MsgBox Err.Number & ":" & Err.Description, vbCritical
Err.Clear
End If
End Sub
-K
vchapran
August 9th, 2001, 12:27 PM
Thanks a lot, everybody.
Vlad
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.