user defined type and collection

G

Guest

I have created a user defined type that I want to store in a collection. I
have run into a couple of problems trying to store the UDT. First, the
collection will only accept the UDT as a string (maybe this really isn't a
problem). Secondly, when I retrieve the UDT from the collection, the only
info I get is what was last written to the UDT. For instance, if I reference
colMyAddress("Terry"), it returns myAddress. Then when I type
myAddress.Street, I get "8345 Sassafras Drive" instead of "2145 Cloverdale
Dr". If I put this UDT in an array everything is fine. But I want to use
the collection's ability to pull information based on the unique key.

Option Compare Database
Option Explicit

Public Type Address
Street As String
City As String
State As String
Zip As String
End Type

Public myAddress As Address

Public Sub LoadAddress()

Set colMyAddress = New Collection
myAddress.Street = "2145 Cloverdale Dr"
myAddress.City = "Colorado Springs"
myAddress.State = "CO"
myAddress.Zip = "80920"
colMyAddress.Add "myAddress", "Terry"

myAddress.Street = "8345 Sassafras Drive"
myAddress.City = "Marysville"
myAddress.State = "WA"
myAddress.Zip = "98271"
colMyAddress.Add "myAddress", "Jay"

End Sub

I appreciate any suggestions anyone has.

Thanks,

Burnsie
 
A

Alex Dybenko

Hi,
build a new class clsAddress intead of UDT (you can just use same public
variables for street, city, etc to make it simple) and use it in collection:

dim oAddress as clsAddress

set oAddress=new clsAddress

oAddress.City="moscow"
oAddress.Street="Cool"
....

colMyAddress.Add oAddress, "Terry"


--
Best regards,
___________
Alex Dybenko (MVP)
http://alexdyb.blogspot.com
http://www.PointLtd.com
 
G

Guest

We were just discussing the advantages of classes over udts (actually
structures since we do some of our coding in VB.Net).

Always great to get some validation from an expert that we are headed in the
right direction.


Thank you!

Burnsie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top