CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    3

    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;
    }}}
    error is in "Private void btnCalculate_Click (object sender, System.EvenArgs e)"

    it says" invalid token void in class, struct, or interface member decleration"

  2. #2
    Join Date
    May 2010
    Posts
    3

    Re: Code error help.....this is my first time using c# so thisis prolly something sim

    i found a error as evenargs should be eventargs...but this still doesnt fix the error im getting

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Code error help.....this is my first time using c# so thisis prolly something sim

    It is a simply formatting issue. You have extra parentheses or you have declared a method directly in a namespace. It appears to be the former, but I don't have time to count braces.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured