|
-
Jan 6th, 2003, 11:56 AM
#1
Thread Starter
Junior Member
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!!!
-
Jan 6th, 2003, 12:39 PM
#2
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.
-
Jan 7th, 2003, 05:03 AM
#3
Thread Starter
Junior Member
So, i can't have it in a form?....can you give an example please?
Thanks
-
Jan 7th, 2003, 11:03 AM
#4
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?
-
Jan 7th, 2003, 11:12 AM
#5
Thread Starter
Junior Member
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?
-
Jan 7th, 2003, 11:28 AM
#6
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.
-
Jan 7th, 2003, 11:38 AM
#7
Thread Starter
Junior Member
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
-
Jan 7th, 2003, 12:30 PM
#8
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.
-
Jan 8th, 2003, 06:09 AM
#9
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|