+ Reply to Thread
Results 1 to 3 of 3

Help with looping through all elements in Class Array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Help with looping through all elements in Class Array

    Hi guys,

    I am trying to loop through all the elements of my class array using the following

    Dim QueryField as cQueryField
    Dim k as integer
    
    Set QueryField = New cQueryField
    
    'POPULATE THE CLASS OBJECT ARRAY
    For k = LBound(myArray,2) to UBound(myArray,2)
    QueryField.PlantMake(k) = myArray(k,2)
    Next k
    
    'LOOP THROUGH THE CLASS OBJECT ARRAY AND DEBUG PRINT THE VALUES
    for i = UBound(QueryField.AllPlantMakes) To UBound(QueryField.AllPlantMakes)
    Debug.Print QueryField.AllPlantMakes(i)
    Next i

    Class
    My Class looks like the following:
    
    Private pAllPlantMakes
    
    Private Sub Class_Initializ()
    ReDim pAllPlantTypes(0)
    End Sub
    
    Public Property Get AllPlantMakes() as String()
    AllPlantMakes = pAllPlantMakes
    End Property
    
    Public Property Get PlantMake(index as Long) as String
    PlantMake = pAllPlantMakes(index)
    End Property
    
    Public Property Let PlantMake(index as Long, strValue as String)
    If index > Ubound(pAllPlantMakes) then ReDim Preserve pAllPlantMakes(index)
    pAllPlantMakes(index) = strValue
    End Poperty

    I keep receiving an error on the line: for i = UBound(QueryField.AllPlantMakes) To UBound(QueryField.AllPlantMakes)

    Why cant I loop through this 'class' array?

    Regards
    Jordan
    Last edited by jordan2322; 04-15-2013 at 06:47 PM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Help with looping through all elements in Class Array

    I'm not really sure how you get to that line in your code since I can't get your code to compile, I also can't help thinking that you'd be better off using a collection or dictionary rather than an array. Maybe like this:
    Private pAllPlantMakes As Collection
    
    Private Sub Class_Initialize()
        Set pAllPlantMakes = New Collection
    End Sub
    
    Public Property Get AllPlantMakes() As Collection
        Set AllPlantMakes = pAllPlantMakes
    End Property
    
    Public Property Get PlantMakeByIndex(ByVal index As Long) As String
        PlantMakeByIndex = pAllPlantMakes(index)
    End Property
    
    Public Sub AddPlant(value As String)
        pAllPlantMakes.Add value
    End Sub
    Sub test()
    
    Dim QueryField As cClass1
    Dim k As Integer
    Dim aPlant As Variant
    
    Dim myArray(0 To 3) As String
    
    myArray(0) = "ABC"
    myArray(1) = "DEF"
    myArray(2) = "GHI"
    myArray(3) = "JKL"
    
    Set QueryField = New cClass1
    
    'POPULATE THE CLASS OBJECT COLLECTION
    For k = LBound(myArray) To UBound(myArray)
        QueryField.AddPlant myArray(k)
    Next k
    
    'LOOP THROUGH THE CLASS OBJECT Collection AND DEBUG PRINT THE VALUES
    For Each aPlant In QueryField.AllPlantMakes
        Debug.Print aPlant
    Next aPlant
    
    'Loop through differently
    For k = 1 To QueryField.AllPlantMakes.Count
        Debug.Print QueryField.PlantMakeByIndex(k)
    Next k
    
    
    End Sub
    Last edited by Kyle123; 04-10-2013 at 03:46 AM.

  3. #3
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Help with looping through all elements in Class Array

    Thanks again Kyle123,

    I have decided to use collections instead of arrays - which is proving to make my life a hell of a lot easier!

    Regards Jordan

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1