Am trying to learn class modules with arrays.
My old way to store 10 garages that can store 50 cars that have 4 wheels. each wheel have a pressure(psi) and a name like "Dunlop"
'wheels_name(garage,car,wheel)
dim wheels_name(10,50,4) as double
dim wheels_psi(10,50,4) as double
my module class attempt :
"wheel" class module
Dim psi As Double
Dim name_str As String
Property Get name() As String
name = name_str
End Property
Property Let name(value As String)
name_str = value
End Property
"car" class module
Dim tab_wheels() As Variant
Public Property Get wheels(index As Long) As String
wheels = tab_wheels(index)
End Property
Public Property Set wheels(index As Long, wheel_object As wheel) 'as wheel? as object?
tab_wheels(index) = wheel_object ' needs set at the beginning?
End Property
Private Sub Class_Initialize()
ReDim tab_wheels(1 To 4) As wheel
End Sub
"garage" class module
Dim tab_cars() As Variant
Public Property Get cars(index As Long) As String
wheels = tab_cars(index)
End Property
Public Property Set cars(index As Long, car_object As Object) 'same doubts as before
Set tab_cars(index) = car_object 'same doubts as before
End Property
Private Sub Class_Initialize()
ReDim tab_cars(1 To 50) As car
End Sub
test sub
dim garages(10) as New garage
Dim i_garage As New garage
Dim i_car As New car
Dim i_wheel As New wheel
i_wheel.name = "Dunlop"
i_car.wheels(1) = i_wheel
[EDIT] fergot to mention this gives me an error
"Compilation error : Impossible to affect a property as read only"(my translation)
"Erreur de compilation" "Impossible d'affecter à une proprieté en lecture seule
[/EDIT]
1/ can I directly access something like this?
garages(2).cars(15).wheels(2).name="Dunlop"
garages(2).cars(15).wheels(2).psi=153.5
with some property modifications.
2/ Is there a better way to do this?
I try to access variable of a module class object arrays of another module class object array.
Am trying to learn class modules with arrays.
My old way to store 10 garages that can store 50 cars that have 4 wheels. each wheel have a pressure(psi) and a name like "Dunlop"
'wheels_name(garage,car,wheel)
dim wheels_name(10,50,4) as double
dim wheels_psi(10,50,4) as double
my module class attempt :
"wheel" class module
Dim psi As Double
Dim name_str As String
Property Get name() As String
name = name_str
End Property
Property Let name(value As String)
name_str = value
End Property
"car" class module
Dim tab_wheels() As Variant
Public Property Get wheels(index As Long) As String
wheels = tab_wheels(index)
End Property
Public Property Set wheels(index As Long, wheel_object As wheel) 'as wheel? as object?
tab_wheels(index) = wheel_object ' needs set at the beginning?
End Property
Private Sub Class_Initialize()
ReDim tab_wheels(1 To 4) As wheel
End Sub
"garage" class module
Dim tab_cars() As Variant
Public Property Get cars(index As Long) As String
wheels = tab_cars(index)
End Property
Public Property Set cars(index As Long, car_object As Object) 'same doubts as before
Set tab_cars(index) = car_object 'same doubts as before
End Property
Private Sub Class_Initialize()
ReDim tab_cars(1 To 50) As car
End Sub
test sub
dim garages(10) as New garage
Dim i_garage As New garage
Dim i_car As New car
Dim i_wheel As New wheel
i_wheel.name = "Dunlop"
i_car.wheels(1) = i_wheel
[EDIT] fergot to mention this gives me an error
"Compilation error : Impossible to affect a property as read only"(my translation)
"Erreur de compilation" "Impossible d'affecter à une proprieté en lecture seule
[/EDIT]
1/ can I directly access something like this?
garages(2).cars(15).wheels(2).name="Dunlop"
garages(2).cars(15).wheels(2).psi=153.5
with some property modifications.
2/ Is there a better way to do this?
I try to access variable of a module class object arrays of another module class object array.
Share Improve this question edited Mar 31 at 20:15 Fdc001 asked Mar 31 at 19:19 Fdc001Fdc001 234 bronze badges New contributor Fdc001 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3 |1 Answer
Reset to default 0I think it's doable:
' Class Module: wheel
Dim psi As Double
Dim name_str As String
Property Get name() As String
name = name_str
End Property
Property Let name(value As String)
name_str = value
End Property
Property Get pressure() As Double
pressure = psi
End Property
Property Let pressure(value As Double)
psi = value
End Property
' Class Module: car
Dim tab_wheels() As wheel
Public Property Get wheels(index As Long) As wheel
Set wheels = tab_wheels(index)
End Property
Public Property Set wheels(index As Long, wheel_object As wheel)
Set tab_wheels(index) = wheel_object
End Property
Private Sub Class_Initialize()
ReDim tab_wheels(1 To 4)
End Sub
' Class Module: garage
Dim tab_cars() As car
Public Property Get cars(index As Long) As car
Set cars = tab_cars(index)
End Property
Public Property Set cars(index As Long, car_object As car)
Set tab_cars(index) = car_object
End Property
Private Sub Class_Initialize()
ReDim tab_cars(1 To 50)
End Sub
Sub Test()
Dim garages(1 To 10) As New garage
Dim i_car As New car
Dim i_wheel As New wheel
i_wheel.name = "Dunlop"
i_wheel.pressure = 153.5
Set i_car.wheels(1) = i_wheel
Set garages(2).cars(15) = i_car
' Accessing the properties
Debug.Print garages(2).cars(15).wheels(1).name
Debug.Print garages(2).cars(15).wheels(1).pressure
End Sub
wheels = tab_cars(index)
?? beware of copy/paste bugs. Is there a better way to do this? - consider a garage to contain acollection
of cars. – CDP1802 Commented Apr 1 at 8:34