[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Question about Using Classes in VB.NET

Posted on 2006-05-27
11
Medium Priority
?
281 Views
Last Modified: 2010-04-23
I'm taking a class in VB.NET and the focus is on classes, and I have to admit, that while I understand the concept, I'm having a hard time getting classes incorporated in my program. For my mortgage calculator, I was able to get one class file working that calculated all the basic, simple calculations. But then I tried to add a class file for the amort table, and things just were not referencing properly. Here is what I ended up with, which works, but as you can see I am rewriting code in the two calculate function. I'd just like some insite on how this could be broken down into smaller segments, using the capabilities of .NET. Any insite will be appreciated :-) This won't affect my grade, but my class is over in a week and I just want a better understanding of how this should work in the real world.

thanks

_________________ Program Code _______________________________________________

Public Class MortgageCalculatorForm

   Dim MortgageTerm As Double
    Dim MortgageAmount As Double
    Dim Payment As Double
    Dim InterestRate As Double

Private Sub MortgageCalculatorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'declares INITIAL Amounts for data entry fields on the screen
        Me.uiMortgageAmountTextBox.Text = ""
        Me.uiDisplayPaymentLabel.Text = ""
        Me.uiAmortTableRichTextBox.Text = ""
        Me.uiMortgageAmountTextBox.Focus()
        Me.uiLoanTypeListBox.SelectedIndex = 0

    End Sub

Private Sub uiCalculatePaymentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiCalculatePaymentButton.Click

        'clicking this button calculates the mortgage payment

        If Me.uiMortgageTermTextBox.Text <> "" AndAlso Me.uiInterestRateTextBox.Text <> "" Then
            Payment = CalculatePayment()
        Else
            Payment = CaculatePaymentTextFile()
        End If

        Me.uiDisplayPaymentLabel.Text = Format(Payment, "currency")
    End Sub

Private Sub uiAmortTableRichTextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uiAmortTableRichTextBox.KeyPress
        'based on Douglass Cole's suggestion and code snippet. Thanks Douglass :-)
        'Suppress the keypress in textbox without disabling the textbox
        e.Handled = True
End Sub

Public Function CalculatePayment()
  'calculates the payment if the user enters information into the text box fields
        Dim MortgageTerm As Double
        Dim MortgageAmount As Double
        Dim Payment As Double
        Dim InterestRate As Double

        MortgageAmount = Val(Me.uiMortgageAmountTextBox.Text)
        MortgageTerm = Val(Me.uiMortgageTermTextBox.Text) ' * 12 'calcualtes the mortgage term based on the value entered in the Mortgage Term Field
        InterestRate = Val(Me.uiInterestRateTextBox.Text) ' / 100) / 12

        Payment = (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))

        'beginning of amort table for this payment calculation
        Dim Balance, MonthlyInterestPayment, MonthlyPrincipalPaid, PrincipalBalance, Rate, NewBalance As Double 'balance of the mortgage after payment is subtracted...not really a true balance because it includes the interest payment.
        Dim NumberPayments = MortgageTerm * 12

        'starting the looping declarations for amort table------------
        NumberPayments = 1
        Balance = MortgageAmount 'original amount
        Rate = (InterestRate / 100) / 12 'annual interest rate

        Do
            MonthlyInterestPayment = Balance * Rate  'calculate monthly interest rate
            MonthlyPrincipalPaid = Payment - MonthlyInterestPayment 'calculate the principle paid for that month
            '
            'new balance subtracting the amount of the monthly principal paid from the new balance
            NewBalance = Balance - MonthlyPrincipalPaid 'declares new balance after last payment was subtracted from running balance

            Me.uiAmortTableRichTextBox.Text = Me.uiAmortTableRichTextBox.Text & NumberPayments.ToString & vbTab & vbTab & MonthlyInterestPayment.ToString("C2") _
            & vbTab & vbTab & MonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & NewBalance.ToString("C2") & vbNewLine

            Balance = NewBalance        'new balance
            NumberPayments = NumberPayments + 1
        Loop Until NumberPayments = MortgageTerm * 12


Return (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))
Return (Me.uiAmortTableRichTextBox.Text = Me.uiAmortTableRichTextBox.Text & NumberPayments.ToString & vbTab & vbTab & MonthlyInterestPayment.ToString("C2") _
        & vbTab & vbTab & MonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & NewBalance.ToString("C2") & vbNewLine)

End Function

Private Function CaculatePaymentTextFile()
'calculates the payment if the mortgage term and rates are chosen from a list box, where the values are in a text file.
        Dim loans As String ' file name
        Dim loanArrayStreamReader As IO.StreamReader ' stream reader to read the file
        Dim splitCharacter As String = ","
        Dim strRates As String '  interest rates.
        Dim strTerms As String '  mortgage term.

        MortgageAmount = Val(Me.uiMortgageAmountTextBox.Text)
        loans = "loans.txt"
        loanArrayStreamReader = New IO.StreamReader(loans)

        'declare the values of the mortgage terms and interest rate variables
        strRates = loanArrayStreamReader.ReadLine() 'reads the interest rates
        'single dimensional array
        strTerms = loanArrayStreamReader.ReadLine() 'reads the mortgage terms
        Dim sarrRates() As String = strRates.Split(splitCharacter)
        Dim sarrTerms() As String = strTerms.Split(splitCharacter)

        If Me.uiLoanTypeListBox.SelectedIndex = 0 Then
             MortgageTerm = sarrTerms(0)
            InterestRate = sarrRates(0)
        ElseIf Me.uiLoanTypeListBox.SelectedIndex = 1 Then
            InterestRate = sarrRates(1)
            MortgageTerm = sarrTerms(1)

        ElseIf Me.uiLoanTypeListBox.SelectedIndex = 2 Then
            InterestRate = sarrRates(2)
            MortgageTerm = sarrTerms(2)
        End If

Payment = (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))

        'beginning of amort table for this calculation - duplication of effort but requires the calculations above to generate the table.

Dim Balance, MonthlyInterestPayment, MonthlyPrincipalPaid, PrincipalBalance, Rate, NewBalance As Double 'balance of the mortgage after payment is subtracted...not really a true balance because it includes the interest payment.

Dim NumberPayments = MortgageTerm * 12

        'starting the looping declarations for amort table  --------------
        NumberPayments = 1
        Balance = MortgageAmount 'original amount
        Rate = (InterestRate / 100) / 12 'annual interest rate

        Do
            MonthlyInterestPayment = Balance * Rate  'calculate monthly interest rate
            MonthlyPrincipalPaid = Payment - MonthlyInterestPayment 'calculate the principle paid for that month
            '
            'new balance subtracting the amount of the monthly principal paid from the new balance
            NewBalance = Balance - MonthlyPrincipalPaid 'declares new balance after last payment was subtracted from running balance

Me.uiAmortTableRichTextBox.Text = Me.uiAmortTableRichTextBox.Text & NumberPayments.ToString & vbTab & vbTab & MonthlyInterestPayment.ToString("C2") _
            & vbTab & vbTab & MonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & NewBalance.ToString("C2") & vbNewLine

            Balance = NewBalance        'new balance
            NumberPayments = NumberPayments + 1
        Loop Until NumberPayments = MortgageTerm * 12



Return (MortgageAmount * (InterestRate / 100) / 12) / (1 - Math.Pow(1 + (InterestRate / 100) / 12, -(MortgageTerm * 12)))

Return (Me.uiAmortTableRichTextBox.Text = Me.uiAmortTableRichTextBox.Text & NumberPayments.ToString & vbTab & vbTab & MonthlyInterestPayment.ToString("C2") _
        & vbTab & vbTab & MonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & NewBalance.ToString("C2") & vbNewLine)

    End Function
End Class
0
Comment
Question by:nflowers1228
  • 5
  • 4
10 Comments
 
LVL 2

Assisted Solution

by:VeryNiceMan
VeryNiceMan earned 1500 total points
ID: 16777994
hi;

your program will run on any programming language in the world; all what we need is changing the syntax!! what does that mean? it means that you are:

- NOT following the new trend in programming which is OOPS (Object Oriented Programming System/Structure)

- NOT utilizing any powers of the .Net world

in a real-world application; everything should be Classes & Objects; understanding Classes in the gateway into the .Net world.

open your project; right click; and select (Add Class...) in Visual Studio.Net and name it MortgageClass

in this class you can add whatever data you like; lets take some examples:

in MortgageClass add the following properties:
- MortgageTerm
- MortgageAmount
- Payment
-InterestRate
etc....

her an example of MortgageTerm property:
Private _MortgageTerm as Double = 0
Public Property MortgageTerm() AS Double
Get
Return _MortgageTerm
End Get
Set(ByVal value As Double)
_MortgageTerm = Value
End Set
End Property

add whatever functions you would like; for example:
Function CalculatePayment(MortgageTerm as Double, MortgageAmount as Double, etc...) as Double
............................. function calculations goes here
End Function

at the end of the day; you'll have a CLASS that does ALL the dirty work for you;

back to your application (we don't worry about calculations now)

whenever you need your class:
Dim m as new MortgageClass
and do whatever you like with (m) !!

by doing this you'll never repeat any code that you wrote before!!; and your code will be much more easier to debug and updated.

hope that helps; if not; keep asking and we both will work on creating the whole application using OOPS.

best regards;
0
 

Author Comment

by:nflowers1228
ID: 16779011
Thanks for the quick response. I did attempt to make this with classes. But the problem was that I couldn't seem to be connected to the amort.vb class. It worked fine with the mortgage.vb class file that calculated all the properties for the payment function.

Here's the mortgage class----------------------------------------------------------------

Public Class MortgageClass
    Public mPayment As Double ' naming payment property - not variable
    Public mMortgageAmount As Double ' naming mortgage amount property - not variable
    Public mInterestRate As Double ' naming interest rate property - not variable
    Public mMortgageTerm As Double ' naming mortage term property - not variable
    Public mLoanType As Integer

    'Dim amortTable As New AmortClass

    Dim LoanTypeArray(,) As Double = {{7, 15, 30}, {5.35, 5.5, 5.75}}
    ' defines the loan type array property
    Public Property LoanType()
        Get
            LoanType = mLoanType
        End Get
        Set(ByVal Value)
            mLoanType = Value
        End Set
    End Property

    ' Calls the calculation and defines the payment property
    Public ReadOnly Property Payment() As String
        Get
            CalculatePayment()
            Payment = mPayment

        End Get
    End Property
    'defines the mortgage amount and provides error handling for the amount entered.
    Public Property MortgageAmount() As Double
        Get
            MortgageAmount = mMortgageAmount
        End Get
        Set(ByVal Value As Double)
            'validate the number is greater than 0 and less than 10000000. Display error message and revert to
            If Value > 0 AndAlso Value < 10000000 Then
                mMortgageAmount = Value

            Else
                mMortgageAmount = -1
                MessageBox.Show("The Mortgage Amount must be greater than zero and less than $1,000,000.", "Monthly Payment Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Set
    End Property
    'mortgage term property
    Public Property MortgageTerm() As Double
        Get
            MortgageTerm = mMortgageTerm
        End Get
        Set(ByVal Value As Double)
            'calculates the mortgage term entered in years as months
            'mMortgageTerm = Value * 12 'original calculation
            mMortgageTerm = Convert.ToInt16(LoanTypeArray(0, mLoanType))

        End Set
    End Property

    'defines the interest rate property
    Public Property InterestRate() As Double
        Get
            InterestRate = mInterestRate
        End Get
        Set(ByVal Value As Double)
            'sets the Interest Rate property to the array
            'mInterestRate = (Value / 100) / 12   'original calculation
            mInterestRate = LoanTypeArray(1, mLoanType)

        End Set
    End Property

    ' defines the CalculatPayment function.
    Public Function CalculatePayment()
        'Return (MortgageAmount * (InterestRate / 12) / (1 - Math.Pow(1 + (InterestRate / 12), -(MortgageTerm * 12))))
        mPayment = -Financial.Pmt((mInterestRate / 12 / 100), mMortgageTerm * 12, mMortgageAmount)

    End Function

End Class

---------------------------Here's the amort class ---------------------------------------

Public Class AmortClass

    Private mNumberPayments As Double ' naming the number of payments
    Private mMonthlyPrincipalPaid As Double ' principal paid for a specific payment
    Private mRate As Double 'annual interest
    Private mCurrentMonthlyInterest As Integer
    Private mMonthlyInterestPayment As Double ' amount of the monthly interest payment

    Private mAmortTable As String ' declariing the amortization table i hope.
    Private mDisplayAmortTable As String ' declariing the amortization table i hope.

    Private mPrincipalBalance As Double 'new balance of the principal  
    Public mBalance As Double ' original amount of the mortgage
    Public mNewBalance As Double ' original amount of the mortgage after calculations
    Public mstuff As String 'not sure what this will do


    Dim mortgagePayment As New MortgageClass
    'Dim mortgageFormClass As New MortgageCalculatorForm

    'property that defines the option number for the radio buttons and array
    Public Property NumberPayments()
        Get
            NumberPayments = mNumberPayments
        End Get
        Set(ByVal Value)
            mNumberPayments = Value
        End Set
    End Property
    Public Property MonthlyPrincipalPaid()
        Get
            MonthlyPrincipalPaid = mMonthlyPrincipalPaid
        End Get
        Set(ByVal Value)
            mMonthlyPrincipalPaid = mortgagePayment.mPayment - mCurrentMonthlyInterest  'amount of the principal paid for that specific month
        End Set
    End Property
    Public Property Rate()
        Get
            Rate = mRate
        End Get
        Set(ByVal Value)
            mRate = (mortgagePayment.mInterestRate / 100) / 12  'annual interest rate
        End Set
    End Property
    Public Property CurrentMonthlyInterest()
        Get
            CurrentMonthlyInterest = mCurrentMonthlyInterest
        End Get
        Set(ByVal Value)
            mCurrentMonthlyInterest = mortgagePayment.MortgageAmount * 12
        End Set
    End Property
    Public Property MonthlyInterestPayment()
        Get
            MonthlyInterestPayment = mMonthlyInterestPayment
        End Get
        Set(ByVal Value)
            mMonthlyInterestPayment = mBalance * mRate
        End Set
    End Property
    'Balance
    Public Property Balance()
        Get
            Balance = mBalance
        End Get
        Set(ByVal Value)
            mBalance = mortgagePayment.MortgageAmount  'original amount
        End Set
    End Property
    'New balance after calculations
    Public Property NewBalance()
        Get
            NewBalance = mNewBalance
        End Get
        Set(ByVal Value)
            mBalance = mortgagePayment.MortgageAmount  'original amount
        End Set
    End Property
    'New balance after calculations
    Public Property stuff()
        Get
            stuff = mstuff
        End Get
        Set(ByVal Value)
            mstuff = mNumberPayments.ToString & vbTab & vbTab & mMonthlyInterestPayment.ToString("C2") _
            & vbTab & vbTab & mMonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & mNewBalance.ToString("C2") & vbNewLine
        End Set
    End Property
    Public ReadOnly Property AmortTable() As String
        Get
            DisplayAmortTable()
            AmortTable = mAmortTable

        End Get
    End Property

    Public Function DisplayAmortTable()
        'create loop to calculate the amortization table
        'this did not work.
        'starting the looping declarations
        NumberPayments() = 1
        mBalance = mortgagePayment.MortgageAmount
        mRate = (mortgagePayment.InterestRate / 100) / 12 'annual interest rate
        mstuff = mNumberPayments.ToString & vbTab & vbTab & mMonthlyInterestPayment.ToString("C2") _
        & vbTab & vbTab & mMonthlyPrincipalPaid.ToString("c2") & vbTab & vbTab & mNewBalance.ToString("C2") & vbNewLine

        Do
            Me.MonthlyInterestPayment = mBalance * mRate  'calculate monthly interest rate
            Me.MonthlyPrincipalPaid = mortgagePayment.mPayment - MonthlyInterestPayment 'calculate the principle paid for that month

            'new balance subtracting the amount of the monthly principal paid from the new balance
            Me.NewBalance = mBalance - MonthlyPrincipalPaid 'declares new balance after last payment was subtracted from running balance

            Me.Balance = mNewBalance        'new balance
            Me.NumberPayments = Me.NumberPayments + 1

        Loop Until Me.NumberPayments = mortgagePayment.MortgageTerm * 12

        Return stuff 'just trying to get something to show up :-)

    End Function
End Class


Here is the main form class -----------------------------------------------------------

Private Sub MortgageCalculatorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'declares INITIAL Amounts for data entry fields on the screen
        Me.uiMortgageAmountTextBox.Text = ""
        'Me.uiPaymentTextBox.Text = ""
        Me.uiDisplayPaymentLabel.Text = ""
        Me.uiAmortTableRichTextBox.Text = ""
        Me.uiMortgageAmountTextBox.Focus()
        Me.uiLoanTypeListBox.SelectedIndex = 0

    End Sub

Private Sub uiCalculatePaymentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiCalculatePaymentButton.Click

        'clicking this button calculates the mortgage payment
        Dim mortgagePayment As New MortgageClass
        Dim amortTable As New AmortClass
        Dim Payment As Double

        'values passed based on the option chosen from the list
        mortgagePayment.MortgageAmount = Format(Me.uiMortgageAmountTextBox.Text, "currency")
        mortgagePayment.LoanType = Me.uiLoanTypeListBox.SelectedIndex
        mortgagePayment.InterestRate = Me.uiLoanTypeListBox.SelectedIndex
        mortgagePayment.MortgageTerm = Me.uiLoanTypeListBox.SelectedIndex

        'clear the amortization table text box
        Me.uiAmortTableRichTextBox.Text = ""

        'variables for amortization table
        Dim Balance As Double 'balance of the mortgage after payment is subtracted...not really a true balance because it includes the interest payment.
        Dim MonthlyInterestPayment As Double ' monthly interest rate paid
        Dim MonthlyPrincipalPaid As Double ' principal paid for a specific payment
        Dim PrincipalBalance As Double 'new balance of the principal
        Dim rate As Double
        Dim NewBalance As Double

        'starting the looping declarations ------------This works but only pulls one properties from the amort.vb class

        amortTable.NumberPayments = 0
        Balance = mortgagePayment.MortgageAmount 'original amount
        rate = (mortgagePayment.InterestRate / 100) / 12 'annual interest rate

        '------------------------   starting the looping declarations from amort.vb ---------------------------------
        amortTable.NumberPayments = 0
        amortTable.Balance = mortgagePayment.MortgageAmount 'original amount
        amortTable.Rate = (mortgagePayment.InterestRate / 100) / 12 'annual interest rate

        Do
            amortTable.MonthlyInterestPayment = Balance * rate  'calculate monthly interest rate
            amortTable.MonthlyPrincipalPaid = mortgagePayment.Payment - MonthlyInterestPayment 'calculate the principle paid for that month

            'new balance subtracting the amount of the monthly principal paid from the new balance
            amortTable.NewBalance = amortTable.Balance - amortTable.MonthlyPrincipalPaid 'declares new balance after last payment was subtracted from running balance

            Me.uiAmortTableRichTextBox2.Text = Me.uiAmortTableRichTextBox2.Text & amortTable.NumberPayments.ToString & vbTab & vbTab & amortTable.MonthlyInterestPayment _
            & vbTab & vbTab & amortTable.MonthlyPrincipalPaid & vbTab & vbTab & amortTable.NewBalance & vbNewLine

            amortTable.Balance = amortTable.NewBalance        'new balance
            amortTable.NumberPayments = amortTable.NumberPayments + 1

        Loop Until amortTable.NumberPayments = mortgagePayment.MortgageTerm * 12

        '------------------------   ending  the looping declarations from amort.vb ---------------------------------

        'display amort table in the uiAmortTableRichTextBox2 I hope.
        'this did not work. The table above and calculations display properly, but when I try to add the information
        'to the amort class, the program holds up. Not sure what is wrong but have commented out so you can see
        'that the table displays if I use the standard form calculations.
        Me.uiAmortTableRichTextBox2.Text = amortTable.DisplayAmortTable

    End Sub

    Private Sub uiAmortTableRichTextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uiAmortTableRichTextBox2.KeyPress
                'Suppress the keypress in textbox without disabling the textbox
        e.Handled = True
    End Sub
End Class


so I'm not sure where I went wrong :-) any insight would be appreciated.
Thanks again!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16779556
Are you having syntax problems or conceptual problems?

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nflowers1228
ID: 16779721
I think it's more of a syntax problem. I was able to get the mortgage.vb class file to work with the main form class file. But I wanted to create an amortization table, so I created a separate class for that, and I can't get any data to display from the properties. I tried to create a return statement in the amort.vb class file and then call it in the main form. When that didn't work, I tried to create the programming part in the main form, calling the properties from the amort.vb class file. Neither worked. When I use the debugger, all the values who as 0. Even when I try to just display a value from the mortgage.vb class file that is referenced in the amort.vb class and that shows a value of 0, (NumberPayments = mortgagePayment.MortgageTerm * 12) even before it calculates with anything, so I know I'm doing something majorly wrong.

Does this clarify what I'm looking for?

Thanks

0
 
LVL 2

Expert Comment

by:VeryNiceMan
ID: 16779995
hi;

i'm sure that somthing VERY simple is wrong, re-producing your forms to catch it; will take a long time. if you kindly post forms and clases in a Zip file; i'll give it a try.

best regards;
0
 

Author Comment

by:nflowers1228
ID: 16780813
I would be happy to, but I can't find a way to add an attachment. Please let me know how and I'll add the files.
0
 
LVL 2

Expert Comment

by:VeryNiceMan
ID: 16782088
hi;

i don'y know about the policy of EE, if it allows this.

try to upload it to any free UPLOAD company like:

http://www.esnips.com
join them, verify email, upload, and post here the link to the file.

there are zillions of websites who offer such FREE service.

yahoo groups and Microsoft Communities are very famous.

best regards;
0
 

Author Comment

by:nflowers1228
ID: 16783717
Thanks, I have created an esnips account and uploaded my files. This is a simplified version, figuring I just need to understand the concept. The mortgage.vb class file works with the main form class file, but I  can't seem to get the properties to calculate when using the values from the mortgage.vb or main form.

Any direction will be appreciated :-)

http://www.esnips.com/web/nflowers1228-School

thanks again.

0
 
LVL 2

Accepted Solution

by:
VeryNiceMan earned 1500 total points
ID: 16784835
hi;

first of all in a real world application you can NOT and should NOT use classes for such extreme simple task!!

second; in a real world application we will NOT use classes for such a simple task. we might use classes only if we would like to sell it as a library for Mortgage and Amortization calculations.

sorry to tell you that your solution and approch for using classes is completly wrong and you did not follow some of the rules for classes; namly Encapsulation. all your variables are declared public, and i can see why you did that; you did it because you couldn't access Mortgage Class from Amortization Class; wawww thats another fatal mistake in Classes World; you simple can NOT do that!!

now lets see; the best solution for your task is as follows; NO classes and NO nothing; just a simple form with click event:

'********************************* START OF CODE
 Private Sub uiCalculatePaymentButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiCalculatePaymentButton.Click
        Dim mAmount As Double
        Dim mInterest As Double
        Dim mYears As Integer
        Dim mMortgage As Double

        mAmount = CType(uiMortgageAmountTextBox.Text.Trim, Double)
        Select Case uiLoanTypeListBox.SelectedIndex
            Case 0
                mYears = 7
                mInterest = 5.35
            Case 1
                mYears = 15
                mInterest = 5.5
            Case 2
                mYears = 30
                mInterest = 5.75
        End Select

        'mortgage claculations
        mMortgage = -Financial.Pmt(mInterest / 12 / 100, mYears * 12, mAmount)
        uiDisplayPaymentLabel.Text = Decimal.Round(mMortgage, 2)

        'amortization calculation
        Dim mMonthlyInterest As Double = mInterest / (12 * 100)
        Dim mNumberOfMonths As Integer = mYears * 12
        Dim mMonthlyPayment As Double = mAmount * (mMonthlyInterest / (1 - (1 + mMonthlyInterest) ^ (-mNumberOfMonths)))

        Dim i As Integer = 1
        Dim h, c, q, p As Double
        While mAmount > 0
            h = mAmount * mMonthlyInterest 'monthly interest
            c = mMonthlyPayment - h 'monthly paid
            q = mAmount - c 'balance
            uiAmortTableRichTextBox.Text &= i & vbTab & Decimal.Round(h, 2) & vbTab & Decimal.Round(c, 2) & vbTab & Decimal.Round(q, 2) & vbCrLf
            mAmount = q
            i += 1
        End While
    End Sub
'********************************* END OF CODE
THATS IT!!!

BUT; if you want to FORCE a CLASSES solution; then I recommend that you create ONE single Class; name it NancyLibrary; this Class would have 3 Prperties and two functions:

Amount Property
InterestRate Property
NumberOfYears Property

a function that calculates the Mortgage and returns Double
a function that claculates Amortization and returns string or array or arraylist or ListView or whatever you like.

keep in mind the following rules:

don't try to shoot a fly with bazooka!! thats called OVER-DESIGN; your solution is very Over Designed; you don't need to that to show your talent.

keep all classes Encapsulated that is all member variables are PRIVATE; to access them use properties

a class should not rely on another class; called Classes Clashes; when more that one class is needed; the calling program do the Maestro.

i'm sure that you can build this class now; but if you like me to help you doing so; i will be more than happy to do that.

hope that helps;
best regards;
0
 

Author Comment

by:nflowers1228
ID: 16785884
Thanks, I was able to get it to work without classes prior to this post. But the project required the use of classes so that's why I was trying to do it. I'm glad that the reason it didn't work is because it wasn't supposed to. At least I know I'm not loosing my mind totally. Hopefully, when I get in the real world, I will have the opportunity to work with classes in the appropriate context.

Thanks for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question