PDA

View Full Version : [SOLVED:] Returning an element from an array of subclasses



dan6219
01-20-2018, 01:10 PM
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

dan6219
01-20-2018, 01:17 PM
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

Paul_Hossler
01-20-2018, 01:35 PM
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]