Solved

C# help

Posted on 2011-03-17
2
262 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now