Solved

C# help

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

11 Experts available now in Live!

Get 1:1 Help Now