I'm trying to understand the correct way to instantiate objects in C#, and I just can't seem to get it right.
My code is in lines 17-37
lines 47-51
and
lines 102-103
Any direction would be genuinely appreciated.
Jim
private void findMortgagePayment()
{
string prompt;
bool validPV = false;
bool validAPR = false;
bool validTerm = false;
double principal = 0.0;
double apr = 0.0 ;
int term = 0;
try
{
//***********************************************************************
//implement input validation and set local variables here
//***********************************************************************
if (validatePrincipalAmount())
{
principal = Double.Parse(txtMortgageAmount.Text);
validPV = true;
}
else
txtMortgageAmount.Clear();
if (validateInterestRate())
{
apr = Double.Parse(txtInterestRate.Text);
validAPR = true;
}
else txtInterestRate.Clear();
if (validateTerm())
{
term = int.Parse(txtMortgageTerm.Text);
validTerm = true;
}
else txtMortgageTerm.Clear();
if (validPV && validAPR && validTerm)
{
//***********************************************************************
//declare and create a new mortgage data object
//invoke this class's displayPayment method
//invoke this class's displayAmoritizationTable method
//***********************************************************************
MortgageData myMortgage = new MortgageData();
displayPayment(myMortgage);
displayAmoritizationTable(myMortgage);
}
else
{
lblAmount.Text = "";
toolTip1.SetToolTip(lblAmount, "");
toolTip1.SetToolTip(rtfAmortTable, "");
if (!validPV)
{
setInputErrorField(txtMortgageAmount);
}
else if (!validAPR)
{
setInputErrorField(txtInterestRate);
}
else
{
setInputErrorField(txtMortgageTerm);
}
}
}
catch (Exception ex)
{
prompt = "Please re-enter the mortgage information and try again";
InputUtilities.showErrorMessage(ex, prompt);
clearFields();
}
}
private void setInputErrorField(TextBox txtBox)
{
string prompt = "Please re-enter the " + txtBox.Tag + " information";
InputUtilities.showErrorMessage(prompt, "Invalid " + txtBox.Tag);
txtBox.Focus();
txtBox.SelectAll();
}
private void displayPayment(MortgageData mortgage)
{
lblAmount.Text = String.Format("{0:c}", mortgage.Payment);
toolTip1.SetToolTip(lblAmount, "Monthly payment is: " + lblAmount.Text);
}
private void displayAmoritizationTable(MortgageData mortgage)
{
string tableOutput = "";
//***********************************************************************
//create AmortizationTable here
//retreive the Amoritization table
//***********************************************************************
AmortizationTable myTable = new AmortizationTable(mortgage);
displayAmoritizationTable(mortgage);
rtfAmortTable.Text = tableOutput;
toolTip1.SetToolTip(rtfAmortTable, "Amortization Table for " + mortgage.FullDataString);
}
#endregion
#region "Form Setup"
private void MortgageInformationInput_Load(object sender, System.EventArgs e)
{
SetDefaultMortgageInformation();
}
private void SetDefaultMortgageInformation()
{
string tlTip;
//set principle value defaults and tool tips
tlTip = "Set Principle value to a number greater than " + PredefinedMortgages.MIN_PV + " and less than or equal to " + PredefinedMortgages.MAX_PV;
toolTip1.SetToolTip(txtMortgageAmount, tlTip);
//set interest defaults and tool tips
tlTip = "Set APR value to a number greater than or equal to " + PredefinedMortgages.MIN_APR + " and less than or equal to " + PredefinedMortgages.MAX_APR;
toolTip1.SetToolTip(txtInterestRate, tlTip);
//set term defaults and tool tips
tlTip = "Set Term value to a number greater than " + PredefinedMortgages.MIN_TERM + " and less than or equal to " + PredefinedMortgages.MAX_TERM;
toolTip1.SetToolTip(txtMortgageTerm, tlTip);
radDefault0.Text = PredefinedMortgages.DefaultMortgageData[0].DataString;
tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[0].FullDataString;
toolTip1.SetToolTip(radDefault0, tlTip);
radDefault1.Text = PredefinedMortgages.DefaultMortgageData[1].DataString;
tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[1].FullDataString;
toolTip1.SetToolTip(radDefault1, tlTip);
radDefault2.Text = PredefinedMortgages.DefaultMortgageData[2].DataString;
tlTip = "Select for loan = " + PredefinedMortgages.DefaultMortgageData[2].FullDataString;
toolTip1.SetToolTip(radDefault2, tlTip);
radDefault0.Checked = true;
}
#endregion
#region "Field Utility Operations"
private void clearFields()
{
try
{
txtMortgageAmount.Clear();
txtInterestRate.Clear();
txtMortgageTerm.Clear();
rtfAmortTable.Clear();
clearErrorProvider();
clearPayment();
}
catch(System.Exception ex)
{
InputUtilities.showErrorMessage(ex, "Delete loan field information and try again");
}
finally
{
txtMortgageAmount.Focus();
}
}
private void clearErrorProvider()
{
try
{
errorProvider1.SetError(txtMortgageAmount, "");
errorProvider1.SetError(txtInterestRate, "");
errorProvider1.SetError(txtMortgageTerm, "");
errorProvider1.SetError(rtfAmortTable, "");
}
catch(System.Exception ex)
{
InputUtilities.clearException(ex);
}
}
private void clearPayment()
{
lblAmount.Text = "";
toolTip1.SetToolTip(lblAmount, "");
}
#endregion
#region "Predefined Mortgage Operations"
private void radDefault0_CheckedChanged(object sender, System.EventArgs e)
{
if (radDefault0.Checked)
{
setMortgageInformation(0);
}
}
private void radDefault1_CheckedChanged(object sender, System.EventArgs e)
{
if (radDefault1.Checked)
{
setMortgageInformation(1);
}
}
private void radDefault2_CheckedChanged(object sender, System.EventArgs e)
{
if (radDefault2.Checked)
{
setMortgageInformation(2);
}
}
private void setMortgageInformation(int selectedMortgage)
{
try
{
clearFields();
if ((selectedMortgage >= PredefinedMortgages.FirstMortgageNumber) && (selectedMortgage <= PredefinedMortgages.LastMortgageNumber))
{
txtMortgageAmount.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Principal.ToString();
txtInterestRate.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].APR.ToString();
txtMortgageTerm.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Term.ToString();
}
else
{
txtMortgageAmount.Text = PredefinedMortgages.DEFAULT_PV.ToString();
txtInterestRate.Text = PredefinedMortgages.DEFAULT_PV.ToString();
txtMortgageTerm.Text = PredefinedMortgages.DEFAULT_TERM.ToString();
}
clearErrorProvider();
}
catch(System.Exception ex)
{
InputUtilities.showErrorMessage(ex, "Unable to set mortgage default mortgage information.");
}
}
#endregion
#region "Input Validation"
private bool nonNumberEntered;
private void txtMortgageAmount_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !validatePrincipalAmount();
}
private bool validatePrincipalAmount()
{
return validateInputField(txtMortgageAmount, PredefinedMortgages.MIN_PV, PredefinedMortgages.MAX_PV);
}
private void txtInterestRate_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !validateInterestRate();
}
private bool validateInterestRate()
{
return validateInputField(txtInterestRate, PredefinedMortgages.MIN_APR, PredefinedMortgages.MAX_APR);
}
private bool validateTerm()
{
return validateInputField(txtMortgageTerm, (double) PredefinedMortgages.MIN_TERM, (double) PredefinedMortgages.MAX_TERM);
}
private bool validateInputField(TextBox txtBox, double min, double max)
{
double val;
string errorMessage;
bool valid;
errorMessage = "Enter a number greater than or equal to " + min
+ " and less than or equal to " + max
+ " in the " + txtBox.Tag;
try
{
errorProvider1.SetError(txtBox, "");
if (txtBox.Text == "")
{
errorProvider1.SetError(txtBox, errorMessage);
valid = false;
}
else
{
val = Double.Parse(txtBox.Text);
if (val < min || val > max)
{
errorProvider1.SetError(txtBox, errorMessage);
valid = false;
}
else
{
errorProvider1.SetError(txtBox, "");
valid = true;
}
}
}
catch(Exception ex)
{
InputUtilities.showErrorMessage(ex, errorMessage);
valid = false;
txtBox.SelectAll();
}
return valid;
}
private void txtInputField_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//stop the character from being entered if not a navigation key or a number
if (nonNumberEntered == true)
{
e.Handled = true;
}
}
private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
TextBox txtBox;
//if a navigation key do not react
try
{
txtBox = (TextBox)(sender);
if (InputUtilities.isNavigationKey(e))
{
errorProvider1.SetError(txtBox, "");
//allow navigation keys to be entered
nonNumberEntered = false;
e.Handled = false;
}
else if (InputUtilities.isNumericKey(e))
{
errorProvider1.SetError(txtBox, "");
nonNumberEntered = false;
e.Handled = false;
}
else
{
string errorMessage = "Please enter only numeric values for the " + txtBox.Tag;
errorProvider1.SetError(txtBox, errorMessage);
//stop value form being entered since it is non-numeric
nonNumberEntered = true;
e.Handled = true;
}
}
catch (Exception ex)
{
//do nothing, catch the error later before calculations are done
InputUtilities.clearException(ex);
}
}
#endregion
private void Label2_Click(object sender, EventArgs e)
{
}
}
}