0

Hi this is my program,supposed to output all methods ,based on "square" input and based on"circle" output case circle.But in any of the scenario only one function gets executed,What did i missed?

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

delegate int OperationDelegate(int a);
namespace DelegationLAB
{
    public delegate void OperationDelegate(int a);

    class Program

    {
        static void SquareArea(int num1)
        {
            Console.WriteLine("SquareArea: {0}" + num1*num1);
        }
        static void SquarePerimetre (int num1)
        {
            Console.WriteLine("PerimeterSquare: {0}" + num1*4);
        }
        static void CubeSurfaceArea(int num1)
        {
            Console.WriteLine("CubeSurfaceArea: {0}" + num1 * 6);
        }
        static void CubeVolume(int num1)
        {
            Console.WriteLine("CubeVolume: is " + num1* num1 * num1);
        }

        static void CircleArea(int num1)

        {

            Console.WriteLine("CircleArea: {0}", 3.14*(num1*num1));
        }
        static void CirclePerimeter(int num1)
        {

            Console.WriteLine("CirclePerimeter: {0}", ( 2 *3.14 )* num1 );

        }
        static void SphereSurfaceArea(int num1)
        {

            Console.WriteLine("SphereSurfaceArea : {0}", (4 * 3.14) * (num1 *num1));

        }

        static void SphereVolume(int num1)
        {

            Console.WriteLine("SphereVolume  : {0}", (1 * (3.14 * num1)) * (1 * (3.14 * num1)) * (1 * (3.14 *num1)));

        }




        static void Main(string[] args)
        {



            Console.WriteLine(  "Enter name of the shape : sqaure or circle:" );

            string answer = Console.ReadLine( );
            Console.Write("Enter a number: ");
            var num = int.Parse(Console.ReadLine());

            OperationDelegate operation ;

                switch ( answer.ToLower() )
                {
                    case "square":

                        operation = SquareArea;
                        operation = SquarePerimetre;
                        operation = CubeSurfaceArea;
                        operation = CubeVolume;
                        break;
                    case "circle":
                        operation = CirclePerimeter;
                        operation = SphereSurfaceArea;
                        operation = SphereVolume;
                        operation = CircleArea;
                        break;
                    default:
                        operation = SquareArea;
                        operation += SquarePerimetre;
                        operation += CubeSurfaceArea;
                        operation += CubeVolume;

                         operation = CirclePerimeter;
                        operation += SphereSurfaceArea;
                        operation += SphereVolume;
                        operation += CircleArea;

                        break;


                }
            operation( num );
        }
    }
}
2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by tinstaafl
0

You didn't follow your own example, in the default case, and add the subsequent methods to operation. Also in the default case you re-initilize operation without running it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
delegate int OperationDelegate(int a);
namespace DelegationLAB
{
    public delegate void OperationDelegate(int a);
    class Program
    {
        static void SquareArea(int num1)
        {
            Console.WriteLine("SquareArea: {0}" + num1 * num1);
        }
        static void SquarePerimetre(int num1)
        {
            Console.WriteLine("PerimeterSquare: {0}" + num1 * 4);
        }
        static void CubeSurfaceArea(int num1)
        {
            Console.WriteLine("CubeSurfaceArea: {0}" + num1 * 6);
        }
        static void CubeVolume(int num1)
        {
            Console.WriteLine("CubeVolume: is " + num1 * num1 * num1);
        }
        static void CircleArea(int num1)
        {
            Console.WriteLine("CircleArea: {0}", 3.14 * (num1 * num1));
        }
        static void CirclePerimeter(int num1)
        {
            Console.WriteLine("CirclePerimeter: {0}", (2 * 3.14) * num1);
        }
        static void SphereSurfaceArea(int num1)
        {
            Console.WriteLine("SphereSurfaceArea : {0}", (4 * 3.14) * (num1 * num1));
        }
        static void SphereVolume(int num1)
        {
            Console.WriteLine("SphereVolume : {0}", (1 * (3.14 * num1)) * (1 * (3.14 * num1)) * (1 * (3.14 * num1)));
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter name of the shape : sqaure or circle:");
            string answer = Console.ReadLine();
            Console.Write("Enter a number: ");
            var num = int.Parse(Console.ReadLine());
            OperationDelegate operation;
            switch (answer.ToLower())
            {
                case "square":
                    operation = SquareArea;
                    operation += SquarePerimetre;
                    operation += CubeSurfaceArea;
                    operation += CubeVolume;
                    break;
                case "circle":
                    operation = CirclePerimeter;
                    operation += SphereSurfaceArea;
                    operation += SphereVolume;
                    operation += CircleArea;
                    break;
                default:
                    operation = SquareArea;
                    operation += SquarePerimetre;
                    operation += CubeSurfaceArea;
                    operation += CubeVolume;
                    operation(num);
                    operation = CirclePerimeter;
                    operation += SphereSurfaceArea;
                    operation += SphereVolume;
                    operation += CircleArea;
                    break;
            }
            operation(num);
            return;
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.