Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# help

Posted on 2011-03-17
2
Medium Priority
?
273 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 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