Consulting

Results 1 to 3 of 3

Thread: Returning an element from an array of subclasses

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location

    Returning an element from an array of subclasses

    Hi,

    I have a a Dimension class which amongst other objects contains an array of a subclass called Element.

    I am struggling to reference a specific Element in the Dimension, getting an error:
    Object variable not set (Error 91)


    Dimension Class:

    Option Explicit
    
    
    Option Base 1
    
    
    ' This class is to store dimension objects
    
    
    Private strServerName As String
    Private strDimName As String
    Private lngElementCount As Long
    Private arrElements() As clsElement
    Private lngElementCounter As Long
    Private arrTest() As String
    
    
    ' Properties
    Public Property Get DimName() As String
        DimName = strDimName
    End Property
    
    
    Public Property Let DimName(DimName As String)
        strDimName = DimName
    End Property
    
    
    Public Property Get ElementCount() As Long
        ElementCount = lngElementCount
    End Property
    
    
    Public Property Get Elements(lngIndex As Long) As clsElement
        Elements = arrElements(lngIndex)
    End Property
    
    
    Public Property Let ServerName(ServerName As String)
        strServerName = ServerName
    End Property
    
    
    Public Sub Initialise(DimName As String)
        Dim lngElementCounter As Long
    
    
        strDimName = DimName
        Call SetServerName
        lngElementCount = Run("DIMSIZ", strServerName & ":" & strDimName)
        ReDim arrElements(1 To lngElementCount) As clsElement
    
    
        ReDim arrTest(1 To lngElementCount) As String
        
        lngElementCounter = 1
        For lngElementCounter = 1 To lngElementCount
            Set arrElements(lngElementCounter) = New clsElement
            Call arrElements(lngElementCounter).Initialise(strServerName, strDimName, lngElementCounter)
            'Debug.Print arrElements(lngElementCounter).ChildCount
        Next lngElementCounter
    End Sub
    
    
    ' Subs/ Functions
    Private Sub SetServerName()
    ' Set the server name if not done so via Let
        If (strServerName = "") Then
            strServerName = Range("server")
        End If
    End Sub
    
    
    Public Sub printElements()
        ' prints the elements in the structure
        
        Dim lngCounter As Long
        
        For lngCounter = 1 To Me.ElementCount
            With arrElements(lngCounter)
                Debug.Print .ElementName, .ChildCount, .index
            End With
        Next lngCounter
    End Sub
    Element Class

    Option Base 1
    
    
    ' This class is to store element objects, parent is dimension
    
    
    Private strServerName As String
    Private strDimName As String
    Private lngIndex As Long
    Private strElementName As String
    Private intChildCount As Integer
    Private colChildren As Collection
    Private colChildrenIndices As Collection
    
    
    ' Properties
    Public Property Get ServerName()
        ServerName = strServerName
    End Property
    
    
    Public Property Get DimName() As String
        DimName = strDimName
    End Property
    
    
    Public Property Get index() As Long
        index = lngIndex
    End Property
    
    
    Public Property Get ElementName() As String
        ElementName = strElementName
    End Property
    
    
    Public Property Get ChildCount() As Integer
        ChildCount = intChildCount
    End Property
    
    
    Public Property Get Children() As Collection
        Set Children = colChildren
    End Property
    
    
    Public Sub Initialise(ServerName As String, DimName As String, lngElementCounter As Long)
        Dim strElName As String
        Dim lngCount As Long
        strDimName = DimName
        strServerName = ServerName
        lngIndex = lngElementCounter
        strElementName = Run("DIMNM", strServerName & ":" & strDimName, lngIndex)
        
        Call Me.setChildren
    End Sub
    
    
    Public Sub setChildren()
        ' Populates the colChildren collection with children of the element
        Dim intCounter As Integer
        Dim strChild As String
        Dim strIndex As String
        
        Set colChildren = New Collection
        
        intChildCount = Run("ELCOMPN", strServerName & ":" & strDimName, strElementName)
        
        For intCounter = 1 To intChildCount
            strChild = Run("ELCOMP", strServerName & ":" & strDimName, strElementName, intCounter)
            strIndex = Run("DIMIX", strServerName & ":" & strDimName, strChild)
            colChildren.Add strChild, strIndex
        Next intCounter
    End Sub
    Testing code:

    Option Explicit
    
    
    Sub TestClasses()
        Dim dimTest As clsDimension
    
    
        Dim dblStart, dblEnd As Double
    
    
        Application.Calculate
    
    
        dblStart = Now()
    
    
        Set dimTest = New clsDimension
    
    
        dimTest.Initialise ("period")
    
    
        dblEnd = Now()
    
    
        Debug.Print dblStart, dblEnd
    
    
        Debug.Print Format(dblEnd - dblStart, "nn:ss")
            
        Call dimTest.printElements 'working fine
        Debug.Print dimTest.DimName, dimTest.ElementCount 'working fine
        Debug.Print dimTest.Elements(1).ElementName ' error 91
           
    End Sub
    I am trying to reference the 1st Element in dimTest Dimension object, and print the name of the element.

    Do I need to do a Set first? I have already done this in the classes themselves so not sure if this is the issue.

    Many thanks in advance

  2. #2
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location
    In case it helps anyone else have managed to resolve it - wasn't using the Set keyword in the get property call.

    should be the following:

    Public Property Get Elements(lngIndex As Long) As clsElement
    Set Elements = arrElements(lngIndex)
    End Property

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by dan6219 View Post
    In case it helps anyone else have managed to resolve it - wasn't using the Set keyword in the get property call.
    Thanks for the update -- you can use [Thread Tools] above your first post to mark it [Solved]
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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