1

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:

  • Resolution
  • Rotation
  • Name
  • Width
  • Height

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

Ben
  • 1,000
  • 2
  • 15
  • 36
  • "Because we now operate in a world of multiple monitors" with "now" being 15 years ago, last millenium :) – Deanna Apr 16 '12 at 15:24

2 Answers2

3

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.

Bob77
  • 13,167
  • 1
  • 29
  • 37
  • +1. Collections should be preferred to arrays IMHO. Property with "index" arg deserves consideration, but I find myself using Collections 9 times out of 10. – MarkJ Apr 17 '12 at 08:00
3

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
wqw
  • 11,771
  • 1
  • 33
  • 41
  • 1
    Also have a look at this question: http://stackoverflow.com/questions/9243461/vb6-and-net-array-differences/9243917#9243917 – Dabblernl Apr 15 '12 at 19:25