Results 1 to 9 of 9

Thread: item collection properties

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31

    Angry item collection properties

    HI, anybody knows how to make something like this???

    collection.item(1).property1=10

    I did the collection part, and it works, but I don't know the proerties part.

    Thanks!!!

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    The "properties part" are not members of the collection class. They are members of the class object contained in the collection.

    Your Collection.Item(?) function would return a reference to the class object at that index in the collection. Once you have a reference to the class object you can then access its members.

    I would suggest reading the "Programming with Objects" section in your MSDN library. It can be found on-line at MS here.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    So, i can't have it in a form?....can you give an example please?
    Thanks

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Well, lets say you have a class module called Testing with a string property called Property1. In your code you could do something like

    VB Code:
    1. Dim colTemp as Collection
    2. Dim objTemp as Testing
    3.  
    4. Set colTemp = New Collection
    5.  
    6. Set objTemp = new Testing
    7. objTemp.Property1 = "Hello World"
    8. colTemp.Add objTemp
    9.  
    10. Set objTemp = new Testing
    11. objTemp.Property1 = "This is a test."
    12. colTemp.Add objTemp
    13.  
    14. debug.print colTemp.Item(1).Property1,colTemp.Item(1).Property1
    15. (or use the shortened syntax)
    16. debug.print colTemp(1).Property1,colTemp(1).Property1
    17.  
    18. set objTemp = Nothing
    19. Set colTemp = Nothing

    Forms are objects. You can add them to a collection as well.

    colForms.Add Form1
    debug.print colForms.Item(1).Caption

    Note that there is a global Forms collection at runtime that is created automatically, is always available and contains forms currently loaded.

    What exactly are you trying to do?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    i'm reading a list of values, from a txt file and i want to put em into a collection , so i can access each item and them each property, like name, address, etc.

    any better idea?

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You can create a class which has Get/Let properties for each column in your text file. Then when you are reading the file you create a new class object and set its properties. Then add the class to your collection.

    Use the sample I gave it may not look like it but it can do exactly what you want.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    so, the idea is...I create a class just to store temporarily the data(for example 3 fields), then I instanciate the class , and assign this one to the collection? I've got the point?
    can you explainme get/let? Is the same to just assign the values to a variable inside the class??



    thanks

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You could use public variables in a class

    Public CustomerName as string
    Public Active as boolean

    The "rules" of object oriented programming state that member variables should not be exposed directly to the outside world but they should be accessed via member functions (Properties in VB).

    Property procedures (Get/Let) are basically functions that let client applications manipulate class member variables. Get is used to retrieve the member variable and Let is used to set its value. The advantage of properties are

    you know exactly when a client is trying to change a member variable
    you can make a member read only by omitting a Property Let
    you can add data validation


    In a class module
    VB Code:
    1. Private mstrName as String
    2. Private mblnActive as Boolean
    3.  
    4. Public Property Get CustomerName() as String
    5.     CustomerName = mstrName
    6. End Property
    7. Public Property Let CustomerName(Byval Value as String)
    8.     mstrName = Value
    9. End Property
    10.  
    11. Public Property Get Active() as Boolean
    12.     Active = mblnActive
    13. End Property
    14. Public Property Let Active(Byval Value as Boolean)
    15.     mblnActive = Value
    16. End Property

    Here is some pseudocode to load a file

    Open File
    Do while not EOF
    Get a Record
    Instantiate a class object
    set the class members
    add the class to a collection
    Loop
    Close file.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Posts
    31
    Great!!! I'll Try that!! Really thanks!!!

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