Ben
Ben

Reputation: 1032

vb6 Class Property as an Array of a Class

Far out, this old vb6 app is killing me. How did I ever develop with this stuff before .NET.

I'm trying to creat a vb6 Class with a Property member being an Array of either UDT or another Class.

e.g.

I have a Class called Monitor which exposes a few properties:

In my main program module I have a class called SystemConfig which has a property called MonitorConfig, but previously it only facilitated one item. Because we now operate in a world of multiple monitors, I need this property to support more than one item.

Unfortunately vb6 doesn't give me List(Of T) so I need the next best thing. My first thought is to use an Array.

Here's my attempt:

Private m_MonitorConfig() As Monitor

Public Property Get MonitorConfig() As Monitor()
    MonitorConfig = m_MonitorConfig
End Property
Public Property Let MonitorConfig(val() As Monitor)
    m_MonitorConfig = val
End Property

How do I get the property to recognise an array value in and out of the MonitorConfig property?

thanks

Upvotes: 1

Views: 5864

Answers (2)

wqw
wqw

Reputation: 11991

Your code is ok but it's not very performant. If you need read-only access to monitors but don't want to implement a full-blown collection, then a simple accessor property and count property would be enough.

Something like this:

Option Explicit

Private Declare Function EmptyMonitorsArray Lib "oleaut32" Alias "SafeArrayCreateVector" (Optional ByVal vt As VbVarType = vbObject, Optional ByVal lLow As Long = 0, Optional ByVal lCount As Long = 0) As Monitor()

Private m_MonitorConfig() As Monitor

Property Get MonitorConfig(ByVal Index As Long) As Monitor
    Set MonitorConfig = m_MonitorConfig(Index)
End Property

Property Get MonitorConfigs() As Long
    MonitorConfigs = UBound(m_MonitorConfig) + 1
End Property

Private Sub Class_Initialize()
    m_MonitorConfig = EmptyMonitorsArray
End Sub

Upvotes: 3

Bob77
Bob77

Reputation: 13267

Either alter the property to accept an "index" argument so you can treat it as "array like" syntactically, or else consider using a Collection instead of an array.

Upvotes: 3

Related Questions