[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.3

VB.NET arrays

Asked by dell64 in Microsoft Visual Basic.Net, VB Objects

Tags: Visual Basic.NET

Hi Experts,

I have a program that is complete and working.....BUT, I would like to be able to associate the correct array with the chosen ComboBox item. For example, if the user choose a loan of 15 Years @ 5.5%, I want the program to be able to calculate the mortgage without having all the if/else statement that TELL the program which array to use based on which the selected index.

Also wondering if there is an easy  to separate some of my code out of the calculate button (into other subs). I tried and could not get it to work. This part is not as important as the above functionallity because getting the above to work would eliminate a lot of the code.

Please note that there is an additional class file in my project that provides the functionality for the amortization in the DataGridView, but is not necessary to post for what I want to do with my arrays.

Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
Option Explicit On
Imports System.Data
Imports System.Collections
Imports System.Collections.Generic
 
Public Class MortgageForm
 
    Private Sub MortgageForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Declare new list
        Dim LoanOpList As New List(Of String)
        'Add items to the list
        LoanOpList.Add("Choose a Loan")
        LoanOpList.Add("7 Years @ 5.35%")
        LoanOpList.Add("15 Years @ 5.5%")
        LoanOpList.Add("30 Years @ 5.75%")
        'set list as the data source for the combo box
        ComboBox1.DataSource = LoanOpList
    End Sub
 
    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        Dim decRate() As Decimal = {0.004458333, 0.00458333, 0.004791666} 'declare and initialize array
        Dim intTerm() As Integer = {84, 180, 360} 'declare and initialize array
        Dim intPymtTerm As Integer
        Dim decPrincipal As Decimal = 0 'declare and initialize principal value
        Dim decPayment, decIntPd, decPrinPd, decPymtRate, decBal As Decimal
 
        Try
 
            Decimal.TryParse(Me.txtPrincipal.Text, decPrincipal)
            If (decPrincipal <= 0) Then 'checks if decimal is entered
                MessageBox.Show("Please Enter a Valid Loan Amount", "Invalid Data!") 'displays error message to user
 
                Me.txtPrincipal.Clear() 'clears principal text box
                Me.txtPrincipal.Focus() 'sets focus to principal text box
 
                Exit Sub 'exits sub
 
            End If
 
            'rate term will be set to the chosen array value
            If ComboBox1.SelectedIndex = 0 Then
                MessageBox.Show("Please Choose a Loan Option", "Alert!")
                Exit Sub
            End If
 
            If ComboBox1.SelectedItem = "7 Years @ 5.35%" Then
                decPymtRate = decRate(0)
                intPymtTerm = intTerm(0)
 
            ElseIf ComboBox1.SelectedItem = "15 Years @ 5.5%" Then
                decPymtRate = decRate(1)
                intPymtTerm = intTerm(1)
 
            ElseIf ComboBox1.SelectedItem = "30 Years @ 5.75%" Then
                decPymtRate = decRate(2)
                intPymtTerm = intTerm(2)
 
            End If
 
            'Calculates the monthly payment
            decPayment = (decPrincipal * decPymtRate) / (1 - Math.Pow(1 + decPymtRate, -intPymtTerm))
            'formats payment to currency
            Me.lblPymtAmt.Text = FormatCurrency(decPayment)
 
            'Declare and instantiate List
            Dim AmortizationList As New List(Of mortgage)
 
            Dim i As Integer ' declare loop variable
            For i = 1 To intPymtTerm  'Will loop to print out amortization     
 
                'set interest
                decIntPd = decPrincipal * decPymtRate
                'set loan amount
                decPrinPd = decPayment - decIntPd
                'set balance
                decBal = decPrincipal - decPrinPd
                'set remaining principal owed
                decPrincipal = decBal
 
                'Adds amortization data to the DataGridView control
                AmortizationList.Add(New mortgage(i, decPrinPd, decIntPd, decBal))
            Next
 
            'Binds data source to the AmortizationList
            Me.MortgageBindingSource.DataSource = AmortizationList
            Me.MortgageDataGridView.DataSource = Me.MortgageBindingSource
 
        Catch ex As Exception 'Prevents program from crashing
            MessageBox.Show(ex.Message.ToString, "Error Handling", MessageBoxButtons.OK)
        End Try
    End Sub
 
    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        lblPymtAmt.Text = "" 'clears text from the label that displays payment
        MortgageDataGridView.Rows.Clear() 'clears the DataGrid control
        ComboBox1.SelectedItem = "7 Years @ 5.35%" 'sets ComboBox control to 1st option
        Me.txtPrincipal.Focus()
 
    End Sub
 
    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close() 'exits program when exit button is clicked
    End Sub
 
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
 
    End Sub
End Class
[+][-]10/31/09 09:05 AM, ID: 25710251Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/01/09 01:08 AM, ID: 25713076Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Visual Basic.Net, VB Objects
Tags: Visual Basic.NET
Sign Up Now!
Solution Provided By: x77
Participating Experts: 2
Solution Grade: A
 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625