Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# help

Posted on 2011-03-17
2
265 Views
Last Modified: 2012-05-11
Hi,

I am learning C# and  I have written a small console program as follows: What  I want is the program to accept a user input that will be either string "A", "S" or "M".

and based on the user input,  a funtion will be passed to a delegate. For eg: if the user enters "A" then function "Add" should be passed to delegated and evaluated and the result should show in the console. But following program does not seem to work and I am not sure what I am doing wrong. Any help is appreciated.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {

        //declare a delegate
        delegate int MethodDelegate(int num1, int num2);
     
       
       



        //Main Function
        static void Main(string[] args)
        {


            if (args.Length < 1)
            {
                Console.WriteLine("Enter an Operation Type (A/S/M):");
                args[0] = Console.ReadLine();

           
            }




            int num1 = 5;
            int num2 = 3;
            string Operation = args[0].ToString();
            MethodDelegate method = null;




            switch (Operation)
            {
                case "A": method = new MethodDelegate(Add);
                    break;
                case "S": method = new MethodDelegate(Subtract);
                    break;
                case "M": method = new MethodDelegate(Multiply);
                    break;

            }

            if (method != null)

                PerformMathOps(method, num1, num2);

            Console.ReadLine();
       
        }




        //Pass an instance of the delegate to a function
        static void PerformMathOps(MethodDelegate method, int num1, int num2)
        {
            Console.WriteLine(method(num1, num2).ToString());

        }




        //Add Function
        static int Add(int x, int y)
        {

            return (x + y);
        }


        //Subtract
        static int Subtract(int a, int b)
        {

            return (a - b);
        }


        //Multiply
        static int Multiply(int c, int d)
        {

            return (c * d);
        }




    }
}


Any help is appreciated,

thanks,
0
Comment
Question by:Techsavy
2 Comments
 
LVL 6

Accepted Solution

by:
nandithaa earned 500 total points
ID: 35162492
Try this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication10
{
    class Program
    {
        //declare a delegate
        delegate int MethodDelegate(int num1, int num2);
        string strOp = "";
        static void Main(string[] args)
        {
            args = new string[1];
            Console.WriteLine("Enter an Operation Type (A/S/M):");
            args[0] = Console.ReadLine();

            int num1 = 5;
            int num2 = 3;
            string Operation = args[0].ToString();
            MethodDelegate method = null;

            switch (Operation)
            {
                case "A": method = new MethodDelegate(Add);
                    break;
                case "S": method = new MethodDelegate(Subtract);
                    break;
                case "M": method = new MethodDelegate(Multiply);
                    break;

            }

            if (method != null)

                PerformMathOps(method, num1, num2);

            Console.ReadLine();
        }

        //Pass an instance of the delegate to a function
        static void PerformMathOps(MethodDelegate method, int num1, int num2)
        {
            Console.WriteLine(method(num1, num2).ToString());

        }

        //Add Function
        static int Add(int x, int y)
        {

            return (x + y);
        }


        //Subtract
        static int Subtract(int a, int b)
        {

            return (a - b);
        }


        //Multiply
        static int Multiply(int c, int d)
        {

            return (c * d);
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Techsavy
ID: 35172343
Worked!!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question