Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am getting an exception while wrting that code fist line of main method
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    delegate int MathFunction(int int1, int int2);
    class Program
    {

        static int Add(int int1, int int2)
        {
            return int1 + int2;
        }

        static int Subtract(int int1, int int2)
        {
            return int1 - int2;
        }

        static int Multiply(int int1, int int2)
        {
            return int1 * int2;
        }

        static int Divide(int int1, int int2)
        {
            return int1 / int2;
        }

        static void PrintResult(MathFunction mathFunction, int int1, int int2)
        {
            int result = mathFunction(int1, int2);
            Console.WriteLine(String.Format("Result is {0}", result));
        }
        static void Main(string[] args)
        {
             <big>int left = Convert.ToInt32(args[0]);</big>  //here i am getting exception
            char theOperator = args[1][0];
            int right = int.Parse(args[2]);
            MathFunction mathFunction;
            if (theOperator == '+')
                mathFunction = Add;
            else if (theOperator == '-')
                mathFunction = Subtract;
            else if (theOperator == '*')
                mathFunction = Multiply;
            else
                mathFunction = Divide;
            PrintResult(mathFunction, left, right);
        }
   
    }
}
Posted
Updated 3-Feb-14 20:51pm
v3

Who told you that args[0] exists? Your exception indicates that you passed zero parameters to the application.
Do you know the purpose of args at all? This is the array of parameters passed. I hope you know how to start applications, as user… :-)
—SA
 
Share this answer
 
Comments
siddharth629 4-Feb-14 2:22am    
http://mrbigglesworth79.blogspot.in/2011/05/delegates-101-part-i-what-is-delegate.html i was reading an artical about delegate an i got that examle... so i am puzzled what i have to do to resolve that ..
Sergey Alexandrovich Kryukov 4-Feb-14 2:28am    
What a second. I answered your question. Did you get it and will accept the answer formally, or you have some questions?
—SA
Peter Leow 4-Feb-14 2:34am    
+5.
Sergey Alexandrovich Kryukov 4-Feb-14 2:37am    
Thank you, Peter.
—SA
Sergey is right. Your program expects some array of arguments when it starts. To pass arguments to your program in visual studio, do the following:
Right click on project > Properties > Debug tag > Start Options > enter arguments
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 4-Feb-14 2:38am    
Yes, a useful advice, a 5. This is how to debug this part using VS...
—SA
You have not supplied any arguments(no values in args array) thats why it shows index out of bound exception.

You can add arguments in below way:
1) Right click on your console application project. Select properties.
2) Then move to Debug tab.
3) Add string(s)/value(s) in commmand line arguments. A line break will end you argument.
4) Save properties.

Supplied values will be your argument array(args[]) which main function takes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900