CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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;
    }}}
    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

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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.
    ===============================
    My Blog

  3. #3
    Join Date
    Oct 2004
    Posts
    206

    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

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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.
    ===============================
    My Blog

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

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

    Quote Originally Posted by eclipsed4utoo View Post
    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
    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