-
March 10th, 2013, 04:57 PM
#1
Load user input into 10x4 rectangular array
Ok, I am working on a form that calculates the future value of an investment. I need to add a 10x4 rectangular array (required) and then display the results when the user clicks exit. I am having trouble figuring out how to load the user input into the array. Here's what I have so far:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FutureValue
{
// This is the starting point for exercise 8-2 from
// "Murach's C# 2010" by Joel Murach
// (c) 2010 by Mike Murach & Associates, Inc.
// www.murach.com
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// TODO: Declare the rectangular array and the row index here
int[,] calculations = new int[10, 4];
int numberOfRows = calculations.GetLength(0);
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
decimal monthlyInvestment =
Convert.ToDecimal(txtMonthlyInvestment.Text);
decimal interestRateYearly =
Convert.ToDecimal(txtInterestRate.Text);
int years = Convert.ToInt32(txtYears.Text);
int months = years * 12;
decimal interestRateMonthly = interestRateYearly / 12 / 100;
decimal futureValue = CalculateFutureValue(
monthlyInvestment, interestRateMonthly, months);
txtFutureValue.Text = futureValue.ToString("c");
txtMonthlyInvestment.Focus();
// TODO: Add the calculation to the rectangular array here
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}
public bool IsValidData()
{
return
// Validate the Monthly Investment text box
IsPresent(txtMonthlyInvestment, "Monthly Investment") &&
IsDecimal(txtMonthlyInvestment, "Monthly Investment") &&
IsWithinRange(txtMonthlyInvestment, "Monthly Investment", 1, 1000) &&
// Validate the Yearly Interest Rate text box
IsPresent(txtInterestRate, "Yearly Interest Rate") &&
IsDecimal(txtInterestRate, "Yearly Interest Rate") &&
IsWithinRange(txtInterestRate, "Yearly Interest Rate", 1, 20) &&
// Validate the Number of Years text box
IsPresent(txtYears, "Number of Years") &&
IsInt32(txtYears, "Number of Years") &&
IsWithinRange(txtYears, "Number of Years", 1, 40);
}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textBox.Focus();
return false;
}
}
public bool IsInt32(TextBox textBox, string name)
{
try
{
Convert.ToInt32(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be an integer.", "Entry Error");
textBox.Focus();
return false;
}
}
public bool IsWithinRange(TextBox textBox, string name,
decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + " must be between " + min
+ " and " + max + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
private decimal CalculateFutureValue(decimal monthlyInvestment,
decimal monthlyInterestRate, int months)
{
decimal futureValue = 0m;
for (int i = 0; i < months; i++)
{
futureValue = (futureValue + monthlyInvestment)
* (1 + monthlyInterestRate);
}
return futureValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
// TODO: Display the rectangular array in a dialog box here
string calculationsString = "";
for (int i = 0; i < calculations.GetLength(0); i++)
{
for (int j = 0; j < calculations.GetLength(1); j++
calculationsString += calculations[i,j] + " ";
calculationsString += "\n";
}
MessageBox.Show(calculationsString, "Future Value Calculations");
this.Close();
}
}
}
int numberOfRows = calculations.GetLength(0); is also giving me an error "A field initializer cannot reference the non-static field, method, or property 'FutureValue.form1.calculations'
Do I need to assign values to the array first?
I already coded the message box at the end, I just mainly need to know how to enter the user input into the array. Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|