how to deal with multi class create from one class

H

Heath P. Dillon

I have a class that I have created that represents a real world
object..(such as 'car')

With this one clsCar I want to create many actually objCar as they are all
the same.

Dim objCar as new clsCar

Each clsCar has a property call VIN, which identifies each car object.
Since I have created many clsCar from this clsCar, how do I in code update a
cars properties.

For example if the objcar that has a vin = 2123 is now sold, I want to
update just that objCar to be objCar.Sold = True

I would figure I first have to reference the specifc objCar for that car, so
I have to find the right objCar (where .vin = 2123) and then update it..

Is my logic poor with this approach?

Thanks for any feedback and ideas......
 
F

Family Tree Mike

Heath said:
I have a class that I have created that represents a real world
object..(such as 'car')

With this one clsCar I want to create many actually objCar as they are
all the same.

Dim objCar as new clsCar

Each clsCar has a property call VIN, which identifies each car object.
Since I have created many clsCar from this clsCar, how do I in code
update a cars properties.

For example if the objcar that has a vin = 2123 is now sold, I want to
update just that objCar to be objCar.Sold = True

I would figure I first have to reference the specifc objCar for that
car, so I have to find the right objCar (where .vin = 2123) and then
update it..

Is my logic poor with this approach?

Thanks for any feedback and ideas......

Well, this would be something similar, assuming VS 2008:

Dim carlot As New List(Of clsCars)
Dim c1 As New clsCars, c2 As New clsCars

c1.VIN = 3
c2.VIN = 241

carlot.Add(c1)
carlot.Add(c2)

' select the first car with the matching vin
Dim sale As clsCars _
= (From c In carlot Where c.VIN = 241 Select c).First
sale.Sold = True
 
J

Joe Cool

Well, this would be something similar, assuming VS 2008:

Dim carlot As New List(Of clsCars)
Dim c1 As New clsCars, c2 As New clsCars

c1.VIN = 3
c2.VIN = 241

carlot.Add(c1)
carlot.Add(c2)

Actually, you don't need two object variables to do that unless you
need o access both at the same time.

Dim car as clsCar

car = new clsCar
car.VIN = 3
carlot.Add(car)
car = new clsCar
car.VIN = 241
carlot.Add(car)

Would accomplish the same thing.
 
J

James Hahn

Actually, VIN should be passed in the constructor, as it appears that there
is no other way to identify which car is being added to the lot, and the
object should not exist without a VIN. So it becomes:

Dim carlot As New List(Of clsCars)
carlot.Add(new clsCars(3))
carlot.Add(new clsCars(241)

Well, this would be something similar, assuming VS 2008:

Dim carlot As New List(Of clsCars)
Dim c1 As New clsCars, c2 As New clsCars

c1.VIN = 3
c2.VIN = 241

carlot.Add(c1)
carlot.Add(c2)

Actually, you don't need two object variables to do that unless you
need o access both at the same time.

Dim car as clsCar

car = new clsCar
car.VIN = 3
carlot.Add(car)
car = new clsCar
car.VIN = 241
carlot.Add(car)

Would accomplish the same thing.
 
H

Harry Strybos

Heath P. Dillon said:
I have a class that I have created that represents a real world
object..(such as 'car')

With this one clsCar I want to create many actually objCar as they are all
the same.

Dim objCar as new clsCar

Each clsCar has a property call VIN, which identifies each car object.
Since I have created many clsCar from this clsCar, how do I in code update
a cars properties.

For example if the objcar that has a vin = 2123 is now sold, I want to
update just that objCar to be objCar.Sold = True

I would figure I first have to reference the specifc objCar for that car,
so I have to find the right objCar (where .vin = 2123) and then update
it..

Is my logic poor with this approach?

Thanks for any feedback and ideas......
I believe a Dictionary of Cars would do the trick. Use the VIN as the key
(string) for the dictionary. The you can use something like:
Cars(VIN).Sold = true
Cars(VIN).Update 'assuming you have an update methode in the Cars class.
 

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