'Visual Basic 2008/2010 Nut van OOP Object Oriented Programming Module Client1 Dim count As Integer Dim capacity As Integer = 16 Dim loans(1, capacity - 1) As Decimal Sub Main() ' add three loan-datagroups to collection : AddLoan(1000, 5) AddLoan(5000, 10) AddLoan(10000, 2) ' remove the second loan-datagroup from the collection : RemoveLoan(1) ' get total payments for all loans : For index As Integer = 0 To count - 1 Dim amount As Decimal = loans(0, index) Dim intrestRate As Decimal = loans(1, index) Console.WriteLine(GetTotalPayment(amount, intrestRate)) Next ' Console.ReadLine() End Sub Sub AddLoan(ByVal amount As Decimal, ByVal intrestRate As Decimal) If count = capacity Then capacity *= 2 ReDim Preserve loans(1, capacity - 1) End If loans(0, count) = amount loans(1, count) = intrestRate count += 1 End Sub Sub RemoveLoan(ByVal index As Integer) Dim shiftIndex As Integer For shiftIndex = index To count - 2 loans(0, shiftIndex) = loans(0, shiftIndex + 1) loans(1, shiftIndex) = loans(1, shiftIndex + 1) Next loans(0, shiftIndex) = 0 loans(1, shiftIndex) = 0 count -= 1 End Sub Function GetTotalPayment(ByVal amount As Decimal, _ ByVal intrestRate As Decimal) As Decimal GetTotalPayment = amount * (1 + intrestRate / 100) End Function End Module Class Loan Private m_Amount As Decimal Private m_IntrestRate As Decimal Public Property Amount() As Decimal Get Amount = m_Amount End Get Set(ByVal value As Decimal) m_Amount = value End Set End Property Public Property IntrestRate() As Decimal Get IntrestRate = m_IntrestRate End Get Set(ByVal value As Decimal) m_IntrestRate = value End Set End Property Public Function GetTotalPayment() As Decimal GetTotalPayment = Amount * (1 + IntrestRate / 100) End Function End Class Class Loans Private m_Count As Integer Private m_Capacity As Integer = 16 Private m_Items(m_Capacity - 1) As Loan Public Sub Add(ByVal item As Loan) If Count = m_Capacity Then m_Capacity *= 2 ReDim Preserve m_Items(m_Capacity - 1) End If m_Items(Count) = item m_Count += 1 End Sub Public Sub RemoveAt(ByVal index As Integer) Dim shiftIndex As Integer For shiftindex = index To Count - 1 m_Items(shiftindex) = m_Items(shiftindex + 1) Next m_Items(shiftIndex) = Nothing m_Count -= 1 End Sub Public ReadOnly Property Count() As Integer Get Count = m_Count End Get End Property Default Public ReadOnly Property Item(ByVal index As Integer) As Loan Get Item = m_Items(index) End Get End Property End Class Module Client2 Sub Main() ' create some new loans Dim loan1 As New Loan With {.Amount = 1000, .IntrestRate = 5} Dim loan2 As New Loan With {.Amount = 5000, .IntrestRate = 10} Dim loan3 As New Loan With {.Amount = 10000, .IntrestRate = 2} ' create new loan-collection Dim loans1 As New Loans ' add created loans to collection loans1.Add(loan1) loans1.Add(loan2) loans1.Add(loan3) ' remove the second loan-datagroup from the collection loans1.RemoveAt(1) ' get total payments for all loans : For index As Integer = 0 To loans1.Count - 1 Console.WriteLine(loans1.Item(index).GetTotalPayment()) Next ' Console.ReadLine() End Sub End Module Module Client3 Dim count As Integer Dim capacity As Integer = 16 Dim loans(2, capacity - 1) As Decimal Sub Main() ' add three loan-datagroups to collection AddLoan(1000, 5, 24) AddLoan(5000, 10, 10) AddLoan(10000, 2, 24) ' remove the second loan-datagroup from the collection RemoveLoan(1) ' get total and monthly payments for all loans : For index As Integer = 0 To count - 1 Dim amount As Decimal = loans(0, index) Dim intrestRate As Decimal = loans(1, index) Dim monthDuration As Integer = loans(2, index) Console.WriteLine(GetTotalPayment(amount, intrestRate) & " " & _ GetMonthlyPayment(amount, intrestRate, monthDuration)) Next ' Console.ReadLine() End Sub Sub AddLoan(ByVal amount As Decimal, ByVal intrestRate As Decimal, _ ByVal monthDuration As Integer) If count = capacity Then capacity *= 2 ReDim Preserve loans(2, capacity - 1) End If loans(0, count) = amount loans(1, count) = intrestRate loans(2, count) = monthDuration count += 1 End Sub Sub RemoveLoan(ByVal index As Integer) Dim shiftIndex As Integer For shiftIndex = index To count - 2 loans(0, shiftIndex) = loans(0, shiftIndex + 1) loans(1, shiftIndex) = loans(1, shiftIndex + 1) loans(2, shiftIndex) = loans(2, shiftIndex + 1) Next loans(0, shiftIndex) = 0 loans(1, shiftIndex) = 0 loans(2, shiftIndex) = 0 count -= 1 End Sub Function GetTotalPayment(ByVal amount As Decimal, _ ByVal intrestRate As Decimal) As Decimal GetTotalPayment = amount * (1 + intrestRate / 100) End Function Function GetMonthlyPayment(ByVal amount As Decimal, _ ByVal intrestRate As Decimal, _ ByVal monthDuration As Integer) As Decimal GetMonthlyPayment = GetTotalPayment(amount, intrestRate) / monthDuration End Function End Module Partial Class Loan Private m_MonthDuration As Integer Public Property MonthDuration() As Integer Get MonthDuration = m_MonthDuration End Get Set(ByVal value As Integer) m_MonthDuration = value End Set End Property Public Function GetMonthlyPayment() As Decimal GetMonthlyPayment = GetTotalPayment() / MonthDuration End Function End Class Module Client4 Sub Main() ' create some new loans Dim loan1 As New Loan With {.Amount = 1000, .IntrestRate = 5, _ .MonthDuration = 24} Dim loan2 As New Loan With {.Amount = 5000, .IntrestRate = 10, _ .MonthDuration = 10} Dim loan3 As New Loan With {.Amount = 10000, .IntrestRate = 2, _ .MonthDuration = 24} ' create new loan-collection Dim loans1 As New Loans ' add created loans to collection loans1.Add(loan1) loans1.Add(loan2) loans1.Add(loan3) ' remove the second loan-datagroup from the collection loans1.RemoveAt(1) ' get total payments for all loans : For index As Integer = 0 To loans1.Count - 1 Console.WriteLine(loans1.Item(index).GetTotalPayment() & " " & _ loans1.Item(index).GetMonthlyPayment()) Next ' Console.ReadLine() End Sub End Module 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2010 - Alle rechten voorbehouden.