foulkrj
asked on
Create objects with Keys or IDs using a Class module.
I want to use a class module to creae instances of objects that can be accessed using a key or ID like this:
set oCurrent object = MyObjects("Mike")
set oCurrent object = MyObjects("Mike")
ASKER
Class MyPeople 'class module
Public sName as string
Public sCity as string
public sPhone as string
set oFollks = new MyPeople
oFolks.sName = "Mike"
oFolks.sCity = "LA"
oFolks.sPhone = "834 7609"
set oFollks = new MyPeople
oFolks.sName = "Sue"
oFolks.sCity = "NY"
oFolks.sPhone = "342 6544"
Kinger247 can i set sName as an ID or Key so I can:
set oCurrentPerson = MyPeople("Mike")
Public sName as string
Public sCity as string
public sPhone as string
set oFollks = new MyPeople
oFolks.sName = "Mike"
oFolks.sCity = "LA"
oFolks.sPhone = "834 7609"
set oFollks = new MyPeople
oFolks.sName = "Sue"
oFolks.sCity = "NY"
oFolks.sPhone = "342 6544"
Kinger247 can i set sName as an ID or Key so I can:
set oCurrentPerson = MyPeople("Mike")
Are you doing this in .net or vb6 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry for the typo, but my first line should be:
ok you need to create a collection of a class or a collection of a type
ok you need to create a collection of a class or a collection of a type
ASKER
In the example code above I left out building a collection to hold the oFolks like
dim allFolks as collection
set oFollks = new MyPeople
oFolks.sName = "Mike"
oFolks.sCity = "LA"
oFolks.sPhone = "834 7609"
allFolks.add oFolks
set oFollks = new MyPeople
oFolks.sName = "Sue"
oFolks.sCity = "NY"
oFolks.sPhone = "342 6544"
allFolks.add oFolks
So how can I set sName to be the ID so I can do this:
set oCurrentPerson = allFolks("Mike")
Does that make the question make sense?
dim allFolks as collection
set oFollks = new MyPeople
oFolks.sName = "Mike"
oFolks.sCity = "LA"
oFolks.sPhone = "834 7609"
allFolks.add oFolks
set oFollks = new MyPeople
oFolks.sName = "Sue"
oFolks.sCity = "NY"
oFolks.sPhone = "342 6544"
allFolks.add oFolks
So how can I set sName to be the ID so I can do this:
set oCurrentPerson = allFolks("Mike")
Does that make the question make sense?
Class MyPeople 'class module
Public sName as string
Public sCity as string
public sPhone as string
---------------
dim temp as mypeople, ofolks as collection
set ofolks =new collection
set temp = new MyPeople
temp.sName = "Mike"
temp.sCity = "LA"
temp.sPhone = "834 7609"
ofolks.add Item:=temp, key:="Mike"
set temp=nothing
set temp= new MyPeople
temp.sName = "Sue"
temp.sCity = "NY"
temp.sPhone = "342 6544"
ofolks.add Item:=temp, key:="Sue"
set temp=nothing
then you can say
ofolks("mike").sphone
and ofolks("sue").sphone
please note that the key should be unique or else you'll get an error
put it something unique... maybe the member id, or fullname
hope this helps
dan
Public sName as string
Public sCity as string
public sPhone as string
---------------
dim temp as mypeople, ofolks as collection
set ofolks =new collection
set temp = new MyPeople
temp.sName = "Mike"
temp.sCity = "LA"
temp.sPhone = "834 7609"
ofolks.add Item:=temp, key:="Mike"
set temp=nothing
set temp= new MyPeople
temp.sName = "Sue"
temp.sCity = "NY"
temp.sPhone = "342 6544"
ofolks.add Item:=temp, key:="Sue"
set temp=nothing
then you can say
ofolks("mike").sphone
and ofolks("sue").sphone
please note that the key should be unique or else you'll get an error
put it something unique... maybe the member id, or fullname
hope this helps
dan
What is it exactly you want to do ?
Heres an example of creating an instance of an object:
Set objWord = CreateObject("Word.Applica
objWord.Visible = True
objWord.Documents.Open ("c:\Testdoc.doc")