Solved

C# help

Posted on 2011-03-17
2
263 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

822 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