CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Collections

  1. #1
    Join Date
    Dec 1999
    Location
    Malaysia
    Posts
    56

    Collections

    Hi,
    I am a bit confused about collections in VB. Can someone explain the difference between collections and classes in VB?

    Thanks for your reply(s)

    ____________________________________
    The VB Bugs in my Life...

  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Collections

    A collection is simply a collection of 'things' - that can be either variants or classes.

    A class is a 'blue-print' for an object (it doesn't become an object until you do a 'set x = new ....' on it).

    A collection can store references to objects internally and can be used to retrieve these object references by a key.

    eg.


    '
    Dim oCollection as Collection
    Dim oCtl as Control
    '
    set oCollection = new Collection
    '
    for Each oCtl In me.Controls
    oCollection.Add oCtl, oCtl.Name
    next
    '
    for Each oCtl In oCollection.Controls
    Debug.print oCtl.Name
    next
    '
    ' Display properties of 'Command1'
    '

    set oCtl = oCollection("Command1")
    '
    Debug.print oCtl.Name
    Debug.print oCtl.Top
    Debug.print oCtl.Left
    '




    The above code goes through all the controls stored on a form and then adds them to another collection object that we create. The code then looks for the 'Command1' control inside our collection and then assigns an object variable to it, it then accesses that object's properties.


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

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