最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - Array of class module object within class module object - Stack Overflow

programmeradmin2浏览0评论

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
  • wheels = tab_cars(index) ?? beware of copy/paste bugs. Is there a better way to do this? - consider a garage to contain a collection of cars. – CDP1802 Commented Apr 1 at 8:34
  • Doh! There are few ugly things in that example. That was just a simplified example of how I plan to use arrays and class modules but I read collections are read only for the basic types. Only thing am worried about now is having to copy an ""add" and "remove" item patterns in every class that have an array. But there might be a way to write it only once for all class module objects arrays. Kind of an abstraction. If anyone knows... – Fdc001 Commented Apr 2 at 5:18
  • Collection items are read only in that you can't change the key/value of an item, but if the item value is a reference to an object then the object itself is not read only. A collections has add/remove methods. – CDP1802 Commented Apr 2 at 15:58
Add a comment  | 

1 Answer 1

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论