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.:rolleyes:
Thanks!!!
Printable View
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.:rolleyes:
Thanks!!!
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.
So, i can't have it in a form?....can you give an example please?
Thanks
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:
Dim colTemp as Collection Dim objTemp as Testing Set colTemp = New Collection Set objTemp = new Testing objTemp.Property1 = "Hello World" colTemp.Add objTemp Set objTemp = new Testing objTemp.Property1 = "This is a test." colTemp.Add objTemp debug.print colTemp.Item(1).Property1,colTemp.Item(1).Property1 (or use the shortened syntax) debug.print colTemp(1).Property1,colTemp(1).Property1 set objTemp = Nothing 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?
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?
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.
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
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:
Private mstrName as String Private mblnActive as Boolean Public Property Get CustomerName() as String CustomerName = mstrName End Property Public Property Let CustomerName(Byval Value as String) mstrName = Value End Property Public Property Get Active() as Boolean Active = mblnActive End Property Public Property Let Active(Byval Value as Boolean) mblnActive = Value 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.
Great!!! I'll Try that!! Really thanks!!!