Solved

Adding to a collection that is in a class

Posted on 2014-04-02
4
352 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

772 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

11 Experts available now in Live!

Get 1:1 Help Now