Multidimensional Arrays as Properties in Class Files?

  • Thread starter Thread starter Trip
  • Start date Start date
T

Trip

Sorry for a second post that is similar to my previous but I did not
receive any direction and I'm fairly well lost here.

Can someone please give me an example of a Let and Get for a
multidimensional array of [say] 10 rows and 2 columns (9, 1).

What is passed back-and-forth to the class - the index and related
values or the array itself?

I'm using Excel XP on one machine and Excel 2002 on another (I assume
it would be that same solution for both versions).

Thanks everyone!

Trip
 
Trip,

Does this help?

Class module

Option Explicit

Private pArray

Public Function GetArray()
GetArray = pArray
End Function

Public Function PutArray(ary())
pArray = ary
End Function

Public Function AddOne()
Dim i As Long
For i = LBound(pArray) To UBound(pArray)
pArray(i, 3) = pArray(i, 3) + 1
Next i
End Function

Private Sub Class_Initialize()
pArray = [{"Bob","M", 123;"Lynne","F",898;"Amy","F",543}]
End Sub


Normal module

Sub TestArrayClass()
Dim myArray
Dim cArray As clsArray

Set cArray = New clsArray

With cArray
myArray = .GetArray
MsgBox myArray(1, 1) & " - " & myArray(1, 3)
.AddOne
myArray = .GetArray
MsgBox myArray(1, 1) & " - " & myArray(1, 3)
End With

End Sub
 
Hey Bob,

Thanks for taking the time to reply. This has helped but doesn't get
me completely out of the woods. I need to use indexes, i.e.;
MyArray(10000,2) and I need to use the index on in the non-class
module.

What I am finding is that if I simply define the array without defining
the the number of rows & columns that I get an error once I try to
store data via an index; i.e.; MyArray(2, 2) = "something". On the
other hand, if I do declare the array with the size parameters I can
not "get" the array stored in the class file; i.e.; MyArray = .GetArray

I find that this is true whether or not I use your method or standard
Public Property Get/Let. So, it seems as though I have to use one
array to set and another array to get - after the get I do a For i =
LBound(MyArray) to UBound(MyArray) swap setting each element in one
array equal to the other, then perform my operations and then set
again.

Any suggestions on how to avoid this?

Thanks!

Trip
 
Trip,

If I am understanding you correctly, why not swap the whole array out of the
class into the module (or vice versa) into a copy, and work on the copy,
then reverse the flow. You can easily tell how many elements the array has,
so there should be no fear of out-of-bounds.
 
Bob,

You are correct in your understanding. Unfortunately, it is I who has
a lack of understanding. Would you be able to provide me an example of
how to swap a whole array with indexes out of AND into a class. I'm
not able to find a way to do both while maintaining the indicies.

This is how I am doing it right now...

In the Class Module cArrayClass...

Private pTestArray as Variant

Public Property Let TestArray(ByVal iTestArray as Variant)
pTestArray = iTestArray
ShiftArray
End Property
Public Property Get TestArray() as Variant
TestArray = pTestArray
End Property


Function ShiftArray

' Just mixes-up the array a little
Dim i As Integer
For i = 1 to 3
pTestArray(i - 1, 0) = pTestArray(i, 0)
Next i

End Function

In a non-Class module...

Sub Test
Dim i As Integer
Dim MyArray(3, 1) as Variant
Dim MyArray2 as Variant
Dim ThisTest as cArrayClass
Set ThisTest = New cArrayClass

MyArray(0, 0) = 1
MyArray(0, 1) = 2
MyArray(1, 0) = 3
MyArray(1, 1) = 4

ThisTest.TestArray = MyArray
MyArray2 = ThisTest.TestArray
' !!! NOTE: MyArray can not accept ThisTest.TestArray !!!

' Swap the array back in element-by-element
For i = LBound(MyArray2) to UBound(MyArray2)
MyArray(i, 0) = MyArray2(i, 0)
MyArray(i, 1) = MyArray2(i, 1)
Next i

For i = LBound(MyArray) to UBound(MyArray)
Msgbox MyArray(i, 0) & ", " & MyArray(i, 1)
Next i

End Sub

(Please excuse any typos - I re-entered and edited this (to provide a
decent example) by hand while looking at another computer. I'm too
tired to plug into my network and map drives right now. I know - this
sounds silly :-))

So, if you have a better way to do this I'm all ears (or eyes as the
case may be).

Thanks Bob!!

Trip
 
Maybe using the Redim statement could help. Without changing the class
module at all I got this to run (no idea if it gave me the desired
results <g>).

Sub Test()
Dim i As Integer
Dim MyArray() As Variant
Dim ThisTest As cArrayClass
Set ThisTest = New cArrayClass

ReDim MyArray(3, 1)
MyArray(0, 0) = 1
MyArray(0, 1) = 2
MyArray(1, 0) = 3
MyArray(1, 1) = 4

ThisTest.TestArray = MyArray
ReDim MyArray(3, 1)
MyArray = ThisTest.TestArray

For i = LBound(MyArray) To UBound(MyArray)
MsgBox MyArray(i, 0) & ", " & MyArray(i, 1)
Next i

End Sub

Hope this helps
Rowan
 
My example did just that Trip. What I didn't do and I am maybe suggesting
that you do is to manipulate it in the module macro, although personally I
would keep it all in the class.

I will knock up a bigger example today if I get a chance and mail it to you.
 
Hey Bob,

I think I'm all set now. Between the great examples you provided and
Rown's contribution along with a lot of experimentation I believe I
have what I need now. Origionally, I though I could simply right a
let/get which would look something like:

Public Property let TestArray(10,000, 1) (ByVal, iTestArray As Double)
As Double

But now I see that won't work and have several ways to work around
this.

Thanks for all you help!!

Trip
 
Back
Top