CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Array or collection

    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


  2. #2
    Join Date
    Jul 2000
    Location
    Hawaii
    Posts
    281

    Re: Array or collection

    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


  3. #3
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Array or collection

    Although your post doesn't help anyhow, thank you anyway.
    Vlad



  4. #4
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Array or collection

    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.
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

  5. #5
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Array or collection

    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



  6. #6
    Join Date
    Jan 2001
    Posts
    165

    Re: Array or collection

    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


  7. #7
    Join Date
    Aug 2000
    Location
    NY, USA
    Posts
    632

    Re: Array or collection

    Thanks a lot, everybody.
    Vlad


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured