Solved

Adding to a collection that is in a class

Posted on 2014-04-02
4
353 Views
Last Modified: 2014-04-03
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 ...
Object variable or With block variable not set

Open in new window

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

Open in new window

This is the relevant parts of my class
Public 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

Open in new window

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.
0
Comment
Question by:qholmberg
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
Steve Dubyo earned 500 total points
ID: 39973460
Hi,

I suspect that you are attempting to .Add to the SubComponent collection before it is instantiated.  You could check for this before attempting to add to it like this..

Public Function AddSubComponent(subComponent As clsComponent)
    If Me.SubComponents Is Nothing Then
        Set Me.SubComponents = New Collection
    End If
    Me.SubComponents.Add subComponent, subComponent.PartNumber
End Function

Open in new window

0
 
LVL 5

Expert Comment

by:Steve Dubyo
ID: 39973503
If my suggestion does work for you I would recommend that rather than doing this check in your AddSubComponent function you could optimise by adding a constructor and instantiating the collection there instead.  This is done by implementing a Class_Initialize method which essentially runs when you create a new instance of your class.  Your class would then look like this..


Public 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.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub Class_Initialize()
    Set Me.SubComponents = New Collection
End Sub

Public Function AddSubComponent(subComponent As clsComponent)
    Me.SubComponents.Add subComponent, subComponent.PartNumber
End Function

Open in new window

0
 

Author Comment

by:qholmberg
ID: 39975069
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 ...
Public SubComponents As Collection

Open in new window

To ...
Public SubComponents As New Collection

Open in new window

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.
0
 
LVL 5

Expert Comment

by:Steve Dubyo
ID: 39975118
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now