Passing Arrays in User Defined Objects

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Private Field in a Class Module that is a string dynamic array. (It
contains a list of part numbers containing letters.)

Is it possible to either -

1) Allow a procedure outside the Class Module to read the entire array
through a Get Property procedure.

2) Allow a procedure outside the Class Module to set the entire array using
a Let/Set Property procedure.

I know how to pass one value at a time through Get and Let, but is it
possible to pass the whole array at once through a Property procedure? I
would prefer not to expose the array (I would like to keep it Private), but I
suppose that is something I could fall back on.
 
Think of the array as a data like a string or integer. The following
works with VB6 -- XL2000 or later (tested only on 2003). For VB5 --
XL97 -- you will have to use a variant because the language doesn't
support an array as a procedure return type.

Option Explicit
Option Base 0

Sub testIt()
Dim x As New Class1, y() As String, z() As String
ReDim y(2)
y(0) = "a": y(1) = "bb": y(2) = "ccc"
x.MyArr = y
z = x.MyArr
MsgBox LBound(x.MyArr) & "," & UBound(x.MyArr) _
& "," & LBound(z) & "," & UBound(z)
MsgBox z(UBound(z))
End Sub

and the class module:
Option Explicit

Private sMyArr() As String
Property Get MyArr() As String()
MyArr = sMyArr
End Property
Property Let MyArr(uMyArr() As String)
sMyArr = uMyArr
End Property

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 
This should help me very much!

Thank you!

Tushar Mehta said:
Think of the array as a data like a string or integer. The following
works with VB6 -- XL2000 or later (tested only on 2003). For VB5 --
XL97 -- you will have to use a variant because the language doesn't
support an array as a procedure return type.

Option Explicit
Option Base 0

Sub testIt()
Dim x As New Class1, y() As String, z() As String
ReDim y(2)
y(0) = "a": y(1) = "bb": y(2) = "ccc"
x.MyArr = y
z = x.MyArr
MsgBox LBound(x.MyArr) & "," & UBound(x.MyArr) _
& "," & LBound(z) & "," & UBound(z)
MsgBox z(UBound(z))
End Sub

and the class module:
Option Explicit

Private sMyArr() As String
Property Get MyArr() As String()
MyArr = sMyArr
End Property
Property Let MyArr(uMyArr() As String)
sMyArr = uMyArr
End Property

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions
 

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

Back
Top