qholmberg
asked on
Adding to a collection that is in a class
I'm writing a quoting application in Excel. It's a multilevel deal ... meaning, components get grouped into assemblies, that get grouped into assemblies, that ... repeat up all the levels.
I've written my own class called clsComponents. For each component, I have a property of SubComponents as type Collection. My intention is to fill that collection with clsComponents that are a sub-component of that particular component (are you still with me?).
I've written a routine that loops through and puts all the subcomponents in an array. It works. However, when I go and loop through the array to assign each sub component to the component, it get an error of ...
This is my routine to assign parent and subcomponent. Ignore the parent stuff as it works.
Thanks.
I've written my own class called clsComponents. For each component, I have a property of SubComponents as type Collection. My intention is to fill that collection with clsComponents that are a sub-component of that particular component (are you still with me?).
I've written a routine that loops through and puts all the subcomponents in an array. It works. However, when I go and loop through the array to assign each sub component to the component, it get an error of ...
Object variable or With block variable not set
Ok ... now for some code.This is my routine to assign parent and subcomponent. Ignore the parent stuff as it works.
For x = 1 To UBound(aryIBOM)
'CREATE COMPONENT RECORD FOR COMPONENT X
Set recComponent = colAssembly.Item(aryIBOM(x, 4))
'FIND THE PARENT COMPONENT AND SET IT FOR COMPONENT X
colAssembly.Item(aryIBOM(x, 4)).Parent = FindParent(x, aryIBOM)
'FIND ALL THE SUBCOMPONENTS OF X AND PLACE THEM IN AN ARRAY
childComponents = FindChildren(x, aryIBOM)
'LOOP THOUGH AND ASSIGN SUBCOMPONENTS TO THE X
For y = 1 To UBound(childComponents)
'CREATE SUBCOMPONENT Y
Set subComponent = colAssembly.Item(childComponents(y))
'ASSIGN SUBCOMPONENT Y TO COMPONENT X
recComponent.AddSubComponent subComponent
Next y
Next x
This is the relevant parts of my classPublic Line As Integer 'LINE COLUMN FROM INPUT BOM
Public LV As Integer 'LV COLUMN FROM INPUT BOM
Public PartNumber As String 'PART# FROM INPUT BOM
Public Rev As String 'REV FROM INPUT BOM
Public Description As String 'DESCRIPTION FROM INPUT BOM
Public Qty As Integer 'QTY FROM INPUT BOM
Public Parent As String 'THE COMPONENT ONE LEVEL ABOVE THIS ONE
Public SubComponents As Collection 'COLLECTION CONTAINING REFERENCE TO ALL SUBCOMPONENTS
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' NAME: AddSubComponent
' AUTHOR: QHOLMBERG
' PURPOSE:
' The class keeps track of all the subcomponents off the assembly
' through a collection. This function adds a subcomponent to
' that collection.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function AddSubComponent(subComponent As clsComponent)
Me.SubComponents.Add subComponent, subComponent.PartNumber
End Function
The error is a result of that line in AddSubComponent function but I'm at a loss as to why. Collections and classes are a little new to me.Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I realized I was not instantiating it last night (prior to seeing this). It was nice to have confirmation this morning. My resolution was even simpler than yours.
Change ...
Change ...
Public SubComponents As Collection
To ...Public SubComponents As New Collection
I do have an initialization routine that sets all the rest of those public properties. I could very easily move this functionality into it. I would have to rearrange the flow of my main program but it is very doable. Thanks for the suggestion.
Thanks for the points, I'm glad you got it working.
Declaring variables As New can lead to problems further down the line, I tend to avoid that construct completely. For what it's worth I don't think it will be an issue in your case but you might want to look into the subject further.
A good explanation can be found here, starting towards the end of page 55.. http://books.google.co.uk/books?id=VnegO0pMYlIC&pg=PA56&lpg=PA56&dq=vba+dim+and+new+in+declaration+Steve+Bullen&source=bl&ots=DvBEMmSeVx&sig=E_MXmPFw3SXIXb4kfLfS9lKNwQ4&hl=en&sa=X&ei=jRZ5T7TID4vZiALQrd2nDg&redir_esc=y#v=onepage&q&f=false
Declaring variables As New can lead to problems further down the line, I tend to avoid that construct completely. For what it's worth I don't think it will be an issue in your case but you might want to look into the subject further.
A good explanation can be found here, starting towards the end of page 55.. http://books.google.co.uk/books?id=VnegO0pMYlIC&pg=PA56&lpg=PA56&dq=vba+dim+and+new+in+declaration+Steve+Bullen&source=bl&ots=DvBEMmSeVx&sig=E_MXmPFw3SXIXb4kfLfS9lKNwQ4&hl=en&sa=X&ei=jRZ5T7TID4vZiALQrd2nDg&redir_esc=y#v=onepage&q&f=false
Open in new window