+ Reply to Thread
Results 1 to 11 of 11

Class with property as collection of another class

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    Class with property as collection of another class

    I've just started to learn how to use classes, so before I explain my problem here, does anyone know of any online resources to learn more about classes in VBA? I read this already: http://www.cpearson.com/excel/Classes.aspx.


    Anyway, here's my problem. I've created a class called CStock. It has 5 standard properties (with Get/Let methods) as explained in that article above. Nothing fancy here.

    I also want to create a CPortfolio class that has 3 properties: pName, pNumberofPositions, and pHoldings. the first 2 are stardard, but the last one is different. I want it to somehow keep a collection of stocks. I tried putting:

    Private pHoldings As Collection
    Public Property Get Holdings() As Collection
        Holdings = pHoldings
    End Property
    
    Public Property Let Holdings(Value As Collection)
        pHoldings = Value
    End Property
    in the CPortfolio module. and then i tried calling:

    port.Holdings.Add stk
    port.Holdings.Add stk2
    in a regular sub, but the sub goes into the Get instead of the Let and i get "Complie Error: Argument Not Optional"


    Anyone know what I'm trying to do here? Cuz I'm not sure I even know. Also, after I get this working, I want to create a CBook class that holds a bunch of portfolios in the same manner. am i going about this the wrong way?

    Thanks,
    Joe
    Last edited by MCCCLXXXV; 11-25-2008 at 12:23 PM. Reason: spelling

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    A collection is an object variable; assignment requires a Set.

    Would you please clean up the spelling of your thread title?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120
    where would i put the set?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello MCCCLXXXV,

    Your definition is creating a private collection in this class. The user will not be able to assign or retrieve values from this collection. Is that what you want? It would be easier to answer your question if you provided an example of what you want to happen.

    Sincerely,
    Leith Ross

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985
    I would create methods to add and remove individual stocks/holdings, and change the Holdings property to:
    Public Property Get Holdings() As Collection
        Set Holdings = pHoldings
    End Property
    if you need to return the entire set in one go.

  6. #6
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120
    I want to have a CPortfolio class that can hold stocks. So, for example, I have 5 portfolios of stocks to manage - each with a different name and a different number of holdings. I want to re-create these 5 portfolios in VBA such that each has its own name and each has its own stocks. In the end, I'd like to be able to access each stock within each portfolio separately, so i can reference stock #12 in portfolio B easily for example. Also, I'd like each stock to be an object of the CStock class, so that I can set multiple attributes for each stock.

    Does that make sense? I'm going to be doing a lot of manipulations within each portfolio and across all portfolios and I just need a simple and clean way to access all the attributes of each portfolio. I tried doing this with multiple-dimension arrays, but it just got ugly and it was hard to remember which attribute was where.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985
    Yes you can have methods in the CPortfolio class to add, remove or retrieve individual CStock items to/from the private collection.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1