PDA

View Full Version : Solved: Class module for two-dimensional array



mikke3141
04-15-2010, 04:31 AM
Hello,

I'm studying the basics for building classes to support an array. One dimensional is already working, but the second dimensions has me in trouble.

Class1 module:
Option Explicit
Private str As Variant
Private MaxNo As Integer
Public Property Get aName(Index As Integer) As Variant
If Index > UBound(str) Or Index < LBound(str) Then
aName = ""
Else
aName = str(Index)
End If
End Property
Public Property Get aMonth(Index As Integer) As Variant
If Index > UBound(str) Or Index < LBound(str) Then
aMonth = ""
Else
aMonth = str(Index)
End If
End Property
Public Property Get Lukum() As Integer
Lukum = MaxNo
End Property
Public Sub fill_array(NameList() As Variant)
Dim ArrayNo As Integer
str = NameList
ArrayNo = UBound(NameList) - LBound(NameList) + 1
MaxNo = ArrayNo
End Sub

Module:
Option Explicit
Function FillIt() As Class1
Dim testobj As Class1
Dim names() As Variant
Dim Month() As Variant
Set testobj = New Class1

names = Array("First", "Second", "Third")
Month = Array("Jan", "Feb", "March")
testobj.fill_array names
testobj.fill_array Month

Set FillIt = testobj

End Function

Sub test()
Dim MyTest As Class1
Dim ArrayNo As Integer

Set MyTest = FillIt

MsgBox "Arrayn size is " & MyTest.Lukum

For ArrayNo = 0 To MyTest.Lukum - 1
MsgBox "2nd array " & ArrayNo + 1 & " is " & MyTest.aMonth(ArrayNo)
Next

For ArrayNo = 0 To MyTest.Lukum - 1
MsgBox "2nd array " & ArrayNo + 1 & " is " & MyTest.aMonth(ArrayNo)
Next
End Sub

I'm trying to add the month also to the array, so that e.g. MyTest.aName(0) would be 'First' and MyTest.aMonth(0) 'Jan'. Now the code writes over the first value, what is should not do. Thank you for your help.

Bob Phillips
04-15-2010, 05:47 AM
Class



Option Explicit

Private str As Variant
Private MaxNo As Long
Public Property Get aName(Index As Integer) As Variant
If Index > UBound(str) Or Index < LBound(str) Then
aName = ""
Else
aName = str(Index, 0)
End If
End Property
Public Property Get aMonth(Index As Integer) As Variant
If Index > UBound(str) Or Index < LBound(str) Then
aMonth = ""
Else
aMonth = str(Index, 1)
End If
End Property
Public Property Get Lukum() As Integer
Lukum = MaxNo
End Property
Public Sub fill_array(NameList() As Variant, MonthList())
Dim i As Long
ReDim str(0 To UBound(NameList), 0 To 1)
For i = LBound(NameList) To UBound(NameList)
str(i, 0) = NameList(i)
str(i, 1) = MonthList(i)
Next i
MaxNo = UBound(NameList) - LBound(NameList) + 1
End Sub
Public Sub fill_array_month(NameList() As Variant)
Dim ArrayNo As Integer
MaxNo = NameList
End Sub


Module



Option Explicit

Function FillIt() As Class1
Dim testobj As Class1
Dim names() As Variant
Dim Month() As Variant
Set testobj = New Class1

names = Array("First", "Second", "Third")
Month = Array("Jan", "Feb", "March")
testobj.fill_array names, Month

Set FillIt = testobj

End Function

Sub test()
Dim MyTest As Class1
Dim ArrayNo As Integer

Set MyTest = FillIt

MsgBox "Arrayn size is " & MyTest.Lukum

For ArrayNo = 0 To MyTest.Lukum - 1
Debug.Print "2nd array " & ArrayNo + 1 & " is " & MyTest.aMonth(ArrayNo)
Debug.Print "2nd array " & ArrayNo + 1 & " is " & MyTest.aName(ArrayNo)
Next
End Sub

mikke3141
04-15-2010, 07:52 AM
Works perfect, Thank You :cool: