none
delegates in c# switch RRS feed

  • General discussion

  • I can not output all statements from switch in both scenarios,even if type square,i expect to see all code executed in switch/case square,but get only 1 line.How to fix it? Some of the delegates are grey out.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    
      delegate int OperationDelegate(int a);
    namespace DelegationLAB
    {
         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 );
    
                
            }
        }
    }
     

    Friday, February 20, 2015 1:27 AM

All replies

  • You need to use Multicast Delegates. I changed your square switch block to

                switch (answer.ToLower())
                {
                    case "square":
                        operation += SquareArea;
                        operation += SquarePerimetre;
                        operation += CubeSurfaceArea;
                        operation += CubeVolume;
                        break;

    And all four delegates are run:

    Enter name of the shape : sqaure or circle:
    square
    Enter a number: 4
    SquareArea: {0}16
    PerimeterSquare: {0}16
    CubeSurfaceArea: {0}24
    CubeVolume: is 64
    Press any key to continue . . .

    Your using of the '=' syntax (delegate) replaces the previous delegate assignment with the current one.

    Friday, February 20, 2015 2:00 AM