+ Reply to Thread
Results 1 to 7 of 7

Thread: Sum of variables in collection

  1. #1
    Registered User
    Join Date
    04-07-2011
    Location
    Gent, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    30

    Thumbs up Sum of variables in collection

    Hi,

    I have a student class: clsStudent

    Public StudentNr As String
    Public Name As String
    Public Invoices As clsInvoices
    I have a invoice class: clsInvoice

    Public Type As String
    Public Number As String
    Public Debit As Double
    Public Credit As Double
    And the last one is a collection of invoices: clsInvoices

    Option Explicit
    Private AllInvoices As New Collection
    
    Public Sub Add(recInvoice As clsInvoice)
        AllInvoices.Add recInvoice
    End Sub
    Public Property Get Count() As Long
        Count = AllInvoices.Count
    End Property
    Public Property Get Items() As Collection
        Set Items = AllInvoices
    End Property
    Public Property Get Item(myItem As Variant) As clsInvoice
        Set Item = AllInvoices(myItem)
    End Property
    Public Sub Remove(myItem As Variant)
        AllInvoices.Remove (myItem)
    End Sub
    As you can see a student can have one (invoice)collection which holds one or multiple invoices.

    I'd like to know how I can make the sum of all debits for one student (or should I say invoicecollection).
    Also I'd like to have the sum of the credit variable.
    The reason is to be able to subtract the total credit from the total debit and to have a TOTAL.

    Maybe I can then access them with something like
    sumDebitStudent = recStudent.Invoices.sumDebit
    sumCreditStudent = recStudent.Invoices.sumCredit
    totalDebitCredit = recStudent.Invoices.TotalDebitCredit
    I'm not sure if I have to make a function in the collection or in the invoice.
    Or should I declare 3 variables inside the invoice class or do I have to do it in the collection class.
    The more I think of it, it might have to be done in student class.

    I'm totally confused.

    Hopefully you understand my gibberish a bit.

    Thank you in advance!
    Last edited by gosa; 01-24-2012 at 07:00 AM. Reason: removed unnecary variables

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Sum of variables in collection

    Personally I'd have private variables in the collections class that add and subtract when you add and remove an invoice from the collection, then read only properties for each one. The total should also be a read only property of the collection class which is the sum of the private debit variable and the sum of the private credit variable.

    Does that make sense?
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Sum of variables in collection

    Actually, scratch that. It would probably just be better to have three read only properties in your collection class that when called, loop through the collection and return the sum
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  4. #4
    Registered User
    Join Date
    04-07-2011
    Location
    Gent, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Sum of variables in collection

    Hi,

    Thanks for trying to help out.

    I tried to add this in the collection class (clsInvoices ) but I'm doing something wrong.

    Private AllInvoices As New Collection
    [COLOR="#0000CD"]Private sumDebit As Double
    Private sumCredit As Double
    Private TotalDebitCredit As Double
    
    Public Property Get getSumDebit() As Double
        Dim recInvoice As New clsInvoice
        
        Set recInvoice = New clsInvoice
        
        For Each recInvoice In AllInvoices.Items
            sumDebit = sumDebit + recInvoice.Debit
        Next recInvoice
       
       getSumDebit = sumDebit
    End Property
    And when I try to get it with recStudent.Invoices.getSumDebit I get a error 438.

    Do you have any suggestion or a piece of code that I can try?
    Last edited by gosa; 01-23-2012 at 02:58 PM.

  5. #5
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Sum of variables in collection

    Almost, you'd need:
    Public Property Get TotalCredit() As Double
    Dim o As clsInvoice
    
    For Each o In AllInvoices
        TotalCredit = TotalCredit + o.Credit
    Next o
    
    End Property
    
    Public Property Get TotalDebit() As Double
    Dim o As clsInvoice
    
    For Each o In AllInvoices
        TotalDebit = TotalDebit + o.Debit
    Next o
    
    End Property
    
    Public Property Get Balance() As Double
    Dim o As clsInvoice
    
    For Each o In AllInvoices
        Balance = Balance + o.Debit + o.Credit
    Next o
    End Property
    Also, although you can just use public variables in classes, having the variables as private but letting and getting them through properties is better practice. It also gives you more control e.g for Debit:

    
    Dim pDebit As Double
    
    Public Property Let Debit(value As Double)
    
    If value < 0 Then
        pDebit = value
    Else
        Err.Raise 1005, , "The value must be less than 0"
    End If
    
    End Property
    
    Public Property Get Debit() As Double
    Debit = pDebit
    End Property
    Last edited by Kyle123; 01-24-2012 at 04:34 AM.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  6. #6
    Registered User
    Join Date
    04-07-2011
    Location
    Gent, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Sum of variables in collection

    Great!

    I will try it out.

    One small question:

    Don't I need to put
    Set 0 = New clsInvoice
    between the DIM and the FOR EACH ?

    Thanks again for the help!!!

    ps:rep added

  7. #7
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Sum of variables in collection

    thanks for the rep

    No, you are not creating a new instance of the class, you are referring to an existing one - in the collection.
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0