Adding a collection to a class object

  • Is this possible does someone know?


    I figure it must be considering the various built in objects that also contain collections, just can't figure out how to pull it all together.


    So to explain the issue:


    I'm moving data between sheets, I have a Dashboard that sits between an Estimation Spreadsheet (Estimates) and a Task Tracking Spreadsheet (Actuals). The dashboard controls the creation of tasks and pulls various data from the other sheets. So for this reason it needs to push or pull data with either the Estimation or Task Tracker.


    Difficulty can come when it's not the initial export to these two sheets but an update.


    In these instances I need to also grab the data that is already on the Target Workbook and amalgamate this with the data coming over from the dashboard.


    I can do this fine on the estimation side of things, but on the Task Tracker their is an undefined number of columns representing the actual Weeks that the Actual Times were spent (week ending format so Friday's date represented as dd/mm/yyyy). So there could be 1 there could be 500 depending on how long the project runs.


    So to get round the unknown element of the Task Tracker I thought about holding each populated column within a collection inside the class.


    So I have a class (clsTaskTrackingData) to represent all data on a row, each of these objects are placed in a collection to record all the rows of data.


    But within this class I also have a collection as one of the properties:



    What I have then done is created another class to represent the WeekEnding Date (which is was going to use as an index to get the data back from the class) and the Number of days spend on that task (which the clsTaskTrackingData represents, the class that this collection is a property of).


    I just can't get my head round the syntax of putting this "sub" class into the collection in the larger class object and then trying to retrieve the data again.


    I'm guessing the let must be something more like:


    Code
    Public Property Let colWeeksTasks(ByRef clsTimeSpendForWeek As TimeSpendForWeek)
        pColWeeksTasks.Add clsTimeSpendForWeek, clsTimeSpendForWeek.strWeekRefKey
    End Property


    Any tips/ideas/even half a clue about the subject I'm slavering my way through?

  • Re: Adding a collection to a class object


    It depends how far you want to go into this, the simplest method is simply to expose the collection on the parent class. The calling code then deals with the inserting.

    Code
    [color=blue]Private[/color] pColWeeksTasks              [color=blue]As[/color] [color=blue]Collection[/color]
    [color=blue]Public[/color] [color=blue]Property[/color] [color=blue]Get[/color] [color=darkcyan]ColWeeksTasks[/color]() [color=blue]As[/color] [color=blue]Collection[/color]: [color=blue]Set[/color] [color=darkcyan]ColWeeksTasks[/color] = pColWeeksTasks: [color=blue]End[/color] [color=blue]Property[/color]


    If you want to have the Parent class deal with adding children then you're probably looking towards a custom collection class, have a look here: http://dailydoseofexcel.com/ar…/custom-collection-class/

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!