Using arrays

loading results to an array

Page 1 of 1

6 Replies - 1368 Views - Last Post: 14 July 2009 - 02:58 PM Rate Topic: -----

#1 MTcage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

Using arrays

Posted 14 July 2009 - 11:07 AM

Just as many other students, I too am working on the mortgage calculator program. I am in my last week of this first .NET Class and I have come to a roadblock, here is my situation... The assignment called for a VB.Net (not web based) program that would accept user input for the loan amount, interest rate, and term of loan, and display the monthly payment of the loan then list the loan balance and interest paid over the term of the loan. the list will be longer than the screen so use loops to display a partial list, hesitate, and then display more of the list. So, I did exactly that and the program works fine. However, after submitting this program for grading, The instructor for the class tells me, "One thing you should work on is not using a timer to pause. You should not use a scroll bar either." He says I should work on loading the results into an array or use a loop with a counter that can be used to display a given number of results with a more results button for displaying more (but without a scroll bar?). Like I said, this program works fine and I am not asking anyone for any specific code (some sort of example would be great but not necessary) What I am asking for is an explanation of how I would go about loading results into an array to be used for displaying just partial results at a time in some way other than I used here:
 Public Class frmMortgageCalculator

	Private Sub frmMortgageCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' Setting control attributes used in the form when it loads
		Me.lblMonthlyPayment.Visible = False
		Me.txtLoanAmount.Focus()
		Me.btnCalculate.Enabled = False

	End Sub
	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
		' Actions that occur when the Calculate button is clicked.
		Me.lblMonthlyPayment.Visible = True
		' Variable declarations.
		Dim strLoanAmount As String
		Dim dblLoanAmount As Double
		Dim strIntRate As String
		Dim dblIntRate As Double
		Dim strLoanTerm As String
		Dim intloanTerm As Integer
		Try
			strLoanAmount = txtLoanAmount.Text
			If Not IsNumeric(strLoanAmount) Then
				MessageBox.Show("Please enter a valid loan amount")
				Me.txtLoanAmount.Clear()
				Me.txtLoanAmount.Focus()
			End If
			dblLoanAmount = Convert.ToDouble(strLoanAmount)
		Catch ex As Exception
		End Try
		Try
			strIntRate = txtIntRate.Text
			If Not IsNumeric(strIntRate) Then
				MessageBox.Show("Please enter a valid interest rate")
				Me.txtIntRate.Clear()
				Me.txtIntRate.Focus()
			End If
			dblIntRate = Convert.ToDouble(strIntRate / 100)
		Catch ex As Exception
		End Try
		Try
			strLoanTerm = txtLoanTerm.Text
			If Not IsNumeric(strLoanTerm) Then
				MessageBox.Show("Please enter a valid term in years")
				Me.txtLoanTerm.Clear()
				Me.txtLoanTerm.Focus()
			End If
			intloanTerm = Convert.ToInt32(strLoanTerm * 12)
		Catch ex As Exception
		End Try
		' Converting APR to monthly rate.
		Dim dblMonthlyRate As Double = dblIntRate / 12
		' Formula for Calculating monthly payments of a loan
		' from data that is input by the user.
		Dim dblMonthlyPayment As Double = dblLoanAmount * (dblMonthlyRate * _
				Math.Pow((1 + dblMonthlyRate), intloanTerm)) / _
				(Math.Pow((1 + dblMonthlyRate), intloanTerm) - 1)

		' Displays the monthly payment in currency format.
		Me.lblMonthlyPayment.Text = FormatCurrency(dblMonthlyPayment)
		' Declaring variable Setting the Payment number to 0.
		Dim intPaymentNumber As Integer = 0
		' Declaring a new variable to be used in the manipulation
		' of two other variables.
		Dim dblBalance As Double = dblLoanAmount - dblMonthlyPayment
		Dim dblInterestPaid As Double = dblBalance * dblMonthlyRate
		Dim intCount As Integer = 0

		' The loop for displaying the payment #, balance, 
		' and interset paid.
		Do
			Me.lblMonthlyPayment.Text = FormatCurrency(dblMonthlyPayment)
			dblInterestPaid = dblBalance * dblMonthlyRate
			intPaymentNumber = intPaymentNumber + 1
			intCount = intCount + 1
			dblBalance -= dblMonthlyPayment - dblBalance * dblMonthlyRate
			txtBalanceList.Text = txtBalanceList.Text & intPaymentNumber _
			& vbTab & "	 " & FormatCurrency(dblBalance) & vbTab & _
			FormatCurrency(dblInterestPaid) & vbNewLine
			' Condition for ending the loop.
			' This is for the very last payment.
			If dblBalance < dblMonthlyPayment Then
				dblMonthlyPayment = dblBalance
				Exit Do
			End If
			' Condition for displaying 15 lines, then pausing
			' for 1000 miliseconds.
			If intCount Mod 12 = 0 Then
				txtBalanceList.Selectionstart = txtBalanceList.Text.Length
				' Scrols content of the control to the
				' current caret position.
				txtBalanceList.ScrollToCaret()
				' Refreshes screen output.
				txtBalanceList.Refresh()
				' Pauses the printing to the screen
				' for better reading.
				System.Threading.Thread.Sleep(500)
			End If
		Loop Until dblMonthlyPayment <= 0.0
	End Sub
	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
		' For clearing the text in all fields 
		' when the Clear button is clicked
		Me.txtLoanAmount.Clear()
		Me.txtIntRate.Clear()
		Me.txtLoanTerm.Clear()
		Me.txtBalanceList.Clear()
		Me.lblMonthlyPayment.Visible = False
	End Sub

	Private Sub txtLoanAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLoanAmount.TextChanged
		'Conditions for enabling the calculate button after
		' valid data is entered into these fields.
		If txtLoanAmount.Text.Length > 0 And txtIntRate.Text.Length > 0 _
		And txtLoanTerm.Text.Length > 0 Then
			btnCalculate.Enabled = True
		Else
			btnCalculate.Enabled = False
		End If
	End Sub

	Private Sub txtIntRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIntRate.TextChanged
		'Conditions for enabling the calculate button after
		' text is entered into these fields.
		If txtLoanAmount.Text.Length > 0 And txtIntRate.Text.Length > 0 _
		And txtLoanTerm.Text.Length > 0 Then
			btnCalculate.Enabled = True
		Else
			btnCalculate.Enabled = False
		End If
	End Sub

	Private Sub txtLoanTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLoanTerm.TextChanged
		'Conditions for enabling the calculate button after
		' text is entered into these fields.
		If txtLoanAmount.Text.Length > 0 And txtIntRate.Text.Length > 0 _
		And txtLoanTerm.Text.Length > 0 Then
			btnCalculate.Enabled = True
		Else
			btnCalculate.Enabled = False
		End If
	End Sub

End Class

Any and all suggestions are greatly appreciated... I just think that my instructor and I speak different languages and perhaps I am not understanding what he is asking of me.

Is This A Good Question/Topic? 0
  • +

Replies To: Using arrays

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Using arrays

Posted 14 July 2009 - 12:30 PM

1 - Store the results as entries in an array of strings, not into the txtBalanceList

2 - Create a listview control sized fro 12 lines and fill it with the first 12 entries from the above array of strings

3 - create and enable/disable as necessary 2 buttons "Next Year" and "Previous Year" that clear the listview and write another 12 (or possibly less) entries. They index into the array either +12 or -12 from the start position - obviously "Previous Year would start disabled.

This disconnects the data creation from the data review.
Was This Post Helpful? 0
  • +
  • -

#3 MTcage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

Re: Using arrays

Posted 14 July 2009 - 12:42 PM

View Postmark.bottomley, on 14 Jul, 2009 - 11:30 AM, said:

1 - Store the results as entries in an array of strings, not into the txtBalanceList

2 - Create a listview control sized fro 12 lines and fill it with the first 12 entries from the above array of strings

3 - create and enable/disable as necessary 2 buttons "Next Year" and "Previous Year" that clear the listview and write another 12 (or possibly less) entries. They index into the array either +12 or -12 from the start position - obviously "Previous Year would start disabled.

This disconnects the data creation from the data review.


Thanks Mark,
This information is very helpful. I completely understand #2 and 3 but my biggest problem here is how do I store the results in an array? I am pretty sure I can figure out how to use the array to display the results... I just can't figure out how to store the results? Do you understand what I mean or am I just getting dense in my old age? :-)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9388
  • View blog
  • Posts: 35,243
  • Joined: 12-June 08

Re: Using arrays

Posted 14 July 2009 - 12:55 PM

REad up on how you create arrays in VB.NET

dim myArrayOfStrings(5) as String



Creates an array (size 5) of strings.

An array list can be used as an dynamically increasing array.
Was This Post Helpful? 0
  • +
  • -

#5 MTcage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-January 09

Re: Using arrays

Posted 14 July 2009 - 01:16 PM

View Postmodi123_1, on 14 Jul, 2009 - 11:55 AM, said:

REad up on how you create arrays in VB.NET

dim myArrayOfStrings(5) as String



Creates an array (size 5) of strings.

An array list can be used as an dynamically increasing array.

Thanks Modi,
I know how to declare arrays and how to assign values to them, what I don't know is how to store results from calculations to an array for use of displaying partial results at a time... I am currently reading the book "Visual_Basic_2005" ( while using Visual Studio 2008) and chapter 9 goes through arrays, I am reading this chapter again and still not getting it.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

Re: Using arrays

Posted 14 July 2009 - 02:42 PM

If you are adding all the elements at one time, you could add all the element into 1 string variable with a comma b/t each value then use a string array to get just the values:
Dim str As String
str &= <someValue> & ","
str &= <someOtherValue> & ","
Dim strArr() As String = str.Split(",")
strArr(0) = <someValue> ' with no comma
strArr(1) = <someOtherValue>


Or poss use generics List(Of T) or an arraylist.
Was This Post Helpful? 0
  • +
  • -

#7 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Using arrays

Posted 14 July 2009 - 02:58 PM

dim monthlies as new arraylist()

monthlies.add(txtBalanceList.Text & intPaymentNumber _
& vbTab & " " & FormatCurrency(dblBalance) & vbTab & _
FormatCurrency(dblInterestPaid))


that should do the adding.

access is monthlies(x) and use monthlies.count to know the total number of lines
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1