Hi,
I have a student class: clsStudent
I have a invoice class: clsInvoicePublic StudentNr As String Public Name As String Public Invoices As clsInvoices
And the last one is a collection of invoices: clsInvoicesPublic Type As String Public Number As String Public Debit As Double Public Credit As Double
As you can see a student can have one (invoice)collection which holds one or multiple invoices.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
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
I'm not sure if I have to make a function in the collection or in the invoice.sumDebitStudent = recStudent.Invoices.sumDebit sumCreditStudent = recStudent.Invoices.sumCredit totalDebitCredit = recStudent.Invoices.TotalDebitCredit
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
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
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
Hi,
Thanks for trying to help out.
I tried to add this in the collection class (clsInvoices ) but I'm doing something wrong.
And when I try to get it with recStudent.Invoices.getSumDebit I get a error 438.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
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.
Almost, you'd need:
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: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
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
Great!
I will try it out.
One small question:
Don't I need to putbetween the DIM and the FOR EACH ?Set 0 = New clsInvoice
Thanks again for the help!!!
ps:rep added
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks