Creating a class with a variable which is an array of objects from an other class.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashwah
    New Member
    • Aug 2007
    • 15

    Creating a class with a variable which is an array of objects from an other class.

    Hi there,

    I have created a class called Pair which has 2 variable, char1 and char2, a bit like a 2d co-ordinate, (2,4),(1,8), etc... This seems to work ok. Heres my code:

    Code:
    Option Compare Database
    
    Private pChar1 As Integer
    Private pChar2 As Integer
    
    Public Property Get Char1() As Integer
        Char1 = pChar1
    End Property
    
    Public Property Let Char1(Value As Integer)
        pChar1 = Value
    End Property
    
    Public Property Get Char2() As Integer
        Char2 = pChar2
    End Property
    
    Public Property Let Char2(Value As Integer)
        pChar2 = Value
    End Property
    I tried to create another class PairArray whose variable is an array of an unspecified number of Pair class objects. I.e. a sequence of co-ordinates, like [(1,3),(6,8),(2, 5),...]. This is the code for that class:

    Code:
    Option Compare Database
    
    Private pPairArray() As cPair
    
    Public Property Get PairArray(i As Integer) As String
       If i >= LBound(pPairArray) And i <= UBound(pPairArray) Then
          PairArray = pPairArray(i)
       End If
    End Property
    
    Public Property Set PairArray(i As Integer, p As cPair)
       If i >= LBound(pPairArray) And i <= UBound(pPairArray) Then
          pPairArray(i) = p
       End If
    End Property
    
    Public Function Resize(i As Integer)
       ReDim pPairArray(1 To i)
    End Function
    I also created a Resize function to change the array size.

    I can create a PairArray object, and call the Resize function ok, but get a 'can't assign to a read only property' error when assigning a Pair to the PairArray using the following function:

    Code:
    Public Function PairArrayTest()
    
    Dim pa As cPairArray
    Set pa = New cPairArray
    
    Dim p As cPair
    Set p = New cPair
    
    p.Char1 = 1
    p.Char2 = 0
    
    
    pa.Resize (10)
    pa.PairArray(1) = p
    
    
    End Function
    Please can anyone shed some light on this?
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Just a thought.
    "PairArray" property is declared to return String and to obtain cPair. This could make VBA think that Get and Set procedures don't belong to the same property.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      How about using an Array of a User Defined Type to store the individual Pairings?
      Code:
      Public Type CharType
        Char1 As Integer
        Char2 As Integer
      End Type
      
      'Hold 25 pairiings of Char1 and Char2
      Public aCharTypes(1 To 50) As CharType
      Code:
      Dim intCtr As Integer
      Dim strBuild As String
      
      For intCtr = 1 To UBound(aCharTypes)            '25 Pairs of User Defined Type CharType
        If intCtr Mod 2 <> 0 Then
          aCharTypes(intCtr).Char1 = 50 * intCtr      'Char1 in Odd Indexes
        Else
          aCharTypes(intCtr).Char2 = 100 * intCtr     'Char2 in Even Indexes
        End If
      Next
      
      'Play Back
      For intCtr = 1 To UBound(aCharTypes)
        If intCtr Mod 2 <> 0 Then
            strBuild = strBuild & "(" & aCharTypes(intCtr).Char1 & ","
        Else
            strBuild = strBuild & aCharTypes(intCtr).Char2 & "),"
        End If
      Next
      
      'All Pairings
      Debug.Print Left$(strBuild, Len(strBuild) - 1)  'Remove Trailing ','
      OUTPUT:
      Code:
      (50,200),(150,400),(250,600),(350,800),(450,1000),(550,1200),(650,1400),(750,1600),(850,1800),(950,2000),(1050,2200),(1150,2400),(1250,2600),(1350,2800),(1450,3000),(1550,3200),(1650,3400),(1750,3600),(1850,3800),(1950,4000),(2050,4200),(2150,4400),(2250,4600),(2350,4800),(2450,5000)

      Comment

      Working...