Consulting

Results 1 to 3 of 3

Thread: Solved: Class module for two-dimensional array

  1. #1
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location

    Solved: Class module for two-dimensional array

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Class

    [vba]

    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
    [/vba]

    Module

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location
    Works perfect, Thank You

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •