Code error help.....this is my first time using c# so thisis prolly something simple
Code:
namespace Exam
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Private void btnCalculate_Click (object sender, System.EvenArgs e)
{
try
{
If (IsValidData());
{
int Numofyrs = Convert.ToInt32(txtnumYrs);
Decimal discountRate = Convert.ToDecimal(txtDuiscountRate);
Decimal yrlyCashFlow = Convert.ToDecimal(txtYrlyCashFlows);
Decimal onetimecosts = Convert.ToDecimal(txtOneTimeCosts);
Decimal yrlyrecurrcosts = Convert.ToDecimal(txtRecurCosts);
npv = CalcNPV(discountRate,yrlyCashFlow,onetimecosts,yrlyrecurrcosts,Numofyrs);
roi = CalcROI(npv,onetimecosts,yrlyrecurrcosts,Numofyrs,discountRate);
bep = CalcBEP(discountRate,yrlyCashFlow,yrlyrecurrcosts,onetimecosts,Numofyrs);
txtOverallNPV.Text=npv.tostring();
txtOverallROI.Text=roi.tostring();
txtBrkEven.Text=bep.tostring();
}
}
catch (Exception ex)
{
MessageBox.Show(ex->Message + "\n\n" +
ex->GetType()->ToString() + "\n" +
ex->StackTrace, "Exception");
}
}
public bool IsValidData()
{
return
IsPresent(txtmNuYears, "Num Yrs") &&
IsInt32(txtNumYears, "Num Yrs") &&
IsWithinRange(txtNumYears, "Num Yrs", 1, 10)&&
IsPresent(txtDuiscountRate, "d rate")&&
IsDecimal(txtDuiscountRate, "d rate")&&
IsRange(txtDuiscountRate, "d rate")&&
IsPresent(txtYrlyCashFlows, "ycf")&&
IsDecimal(txtYrlyCashFlows, "ycf")&&
IsRange(txtYrlyCashFlows, "ycf")&&
IsPresent(txtOneTimeCosts, "otc")&&
IsDecimal(txtOneTimeCosts, "otc")&&
IsRange(txtOneTimeCosts, "otc")&&
IsPresent(txtRecurCosts, "rec cost")&&
IsDecimal(txtRecurCosts, "rec cost")&&
IsRange(txtRecurCosts, "rec cost");
}
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 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, double min, double max)
{
double number = Convert.ToDouble(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 CalcNPV(decimal rate, decimal cashflow, decimal onetime, decimal recurring, int year)
{
double sum = 0;
do
{
sum += ((Convert.ToDouble(cashflow - recurring)) / Math.Pow(Convert.ToDouble(1 + rate), Convert.ToDouble(year)));
year--;
} while (year > 0);
return Convert.ToDecimal(sum - Convert.ToDouble(onetime));
}
private decimal CalcROI(decimal npv, decimal recurring, decimal onetime, int year, decimal rate)
{
double sum = 0;
do
{
sum += ((Convert.ToDouble(recurring)) / Math.Pow(Convert.ToDouble(1 + rate), Convert.ToDouble(year)));
year--;
} while (year > 0);
return Convert.ToDecimal(Convert.ToDouble(npv) / (Convert.ToDouble(onetime) + sum));
}
private int CalcBEP(decimal rate, decimal cashflow, decimal recurring, decimal onetime, int year)
{
double sum1 = 0;
double sum2 = 0;
int i = 0;
do
{
sum1 += ((Convert.ToDouble(cashflow)) / Math.Pow(Convert.ToDouble(1 + rate), Convert.ToDouble(year)));
sum2 += ((Convert.ToDouble(recurring)) / Math.Pow(Convert.ToDouble(1 + rate), Convert.ToDouble(year)));
i++;
if ((sum1 - sum2) - Convert.ToDouble(onetime) > 0)
break;
else if (i == 100)
break;
else
onetime -= Convert.ToDecimal((sum1 - sum2));
}while(i >= 0);
return i;
}}}
the error is in "Private void btnCalculate_Click (object sender, System.EvenArgs e)"
it says "invalid token void in class, struct, or interface member decleration
please any help is appreciated
Re: Code error help.....this is my first time using c# so thisis prolly something sim
where do I start?...
Code:
MessageBox.Show(ex->Message + "\n\n" +
ex->GetType()->ToString() + "\n" +
ex->StackTrace, "Exception");
"->" is not valid in C#. the code should be this..
Code:
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
==========================================
Code:
If (IsValidData());
C# is a case-sensitive language. There is no "If"(capital "I") statement, but there is an "if"(lowercase "i") statement.
You also shouldn't have the semicolon at the end of the if statement. That is basically terminating the if statement.
Code:
if (IsValidData())
{
// your code
}
=============================================
Code:
txtOverallNPV.Text=npv.tostring();
txtOverallROI.Text=roi.tostring();
txtBrkEven.Text=bep.tostring();
These should be ".ToString()".
=============================================
I didn't go through the rest of the code.
Re: Code error help.....this is my first time using c# so thisis prolly something sim
You're also missing a 't' in System.EvenArgs.
It should read System.EventArgs
Re: Code error help.....this is my first time using c# so thisis prolly something sim
And just to add, you should really use IntelliSense. It's there for a reason.
Re: Code error help.....this is my first time using c# so thisis prolly something sim
Quote:
Originally Posted by
eclipsed4utoo
And just to add, you should really use IntelliSense. It's there for a reason.
In my day we only had terminals with a max column width of 28 characters and we walked to work 30 miles in the snow :)