none
i want to perform casting but getting error..plz help RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace casting
    {
        class Program
        {
            static void Main(string[] args)

            {
                shape[] shapes = { new circle() ,   };
                circle cric = shapes[1] as circle;           
              
                if (cric != null)
                {

                    try{
                          Console.WriteLine("cast is sucessful");
                        }
                    catch (Exception IndexOutOfRangeException)

                        {
                        Console.WriteLine("IndexOutOfRangeException caught");
                        }
                } 
            }

        }
    }
    class shape
    {
        public void draw()
        {
            Console.WriteLine("drawing shape");
        }
    }

    class circle : shape
    {
        public new void draw()
        {
            Console.WriteLine(" drawing circle");
        }
    }


                 

       
                
                
                
    Sunday, December 30, 2012 4:58 PM

Answers

  • Your index is out of range.  Try this:
     
        shape [] shapes = new shape [] {new circle()};
        circle circ = (circle) shapes [0]; // 0 is the first shape.

    --
    Mike
    • Proposed as answer by TSoftware-Old Sunday, December 30, 2012 5:09 PM
    • Marked as answer by love4csharp Sunday, December 30, 2012 5:13 PM
    Sunday, December 30, 2012 5:07 PM
  • Hi,

    your error handling doesnt make any sense. Try:

    shape[] shapes = { new circle(), };
    
    			
    
    try {
    	circle cric = shapes[1] as circle;
    	if (cric != null) {
    		Console.WriteLine("cast is sucessful");
    	}
    } catch (IndexOutOfRangeException e) {
    	Console.WriteLine("IndexOutOfRangeException caught;" + e.Message);
    }

    I kept your array mistake for demonstration purposes.

    Regards,

    wizend


    • Edited by Wizend Sunday, December 30, 2012 5:20 PM
    • Marked as answer by love4csharp Sunday, December 30, 2012 5:25 PM
    Sunday, December 30, 2012 5:18 PM

All replies

  • Your index is out of range.  Try this:
     
        shape [] shapes = new shape [] {new circle()};
        circle circ = (circle) shapes [0]; // 0 is the first shape.

    --
    Mike
    • Proposed as answer by TSoftware-Old Sunday, December 30, 2012 5:09 PM
    • Marked as answer by love4csharp Sunday, December 30, 2012 5:13 PM
    Sunday, December 30, 2012 5:07 PM
  • Hi,

    First, let me note that in C#, by convention, all namespeces', methods', properties', and classes' names should start with an uppercase (Draw() instead of draw()).

    You are casting in the line

    circle cric = shapes[1] as circle;

    and that's what should be inside the try block, because that's what could possibly not work. Also, every catch block should provide, besides the expected exceptions catchers, a general catcher in the end ( catch (Exception e) { /* general catching...*/ }).

    An IndexOutOfRangeException will pop up uncaught because (as I stated before), the line is outside the try block. But you should not catch it but prevent it (as it is a boneheaded exception :P). Remember that arrays are 0-based, the first element is accessed through shapes[0], not shapes[1]. That also means you can remove your try-catch block.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    • Proposed as answer by JMCF125 Sunday, December 30, 2012 5:33 PM
    Sunday, December 30, 2012 5:13 PM
  • thanks for identifying my minor mistake.

    Sunday, December 30, 2012 5:13 PM
  • Hi,

    your error handling doesnt make any sense. Try:

    shape[] shapes = { new circle(), };
    
    			
    
    try {
    	circle cric = shapes[1] as circle;
    	if (cric != null) {
    		Console.WriteLine("cast is sucessful");
    	}
    } catch (IndexOutOfRangeException e) {
    	Console.WriteLine("IndexOutOfRangeException caught;" + e.Message);
    }

    I kept your array mistake for demonstration purposes.

    Regards,

    wizend


    • Edited by Wizend Sunday, December 30, 2012 5:20 PM
    • Marked as answer by love4csharp Sunday, December 30, 2012 5:25 PM
    Sunday, December 30, 2012 5:18 PM
  • thanks JM

    i will follow convention.

    Sunday, December 30, 2012 5:26 PM
  • thanks wizend nice ans
    Sunday, December 30, 2012 5:27 PM
  • Also, every catch block should provide, besides the expected exceptions catchers, a general catcher in the end ( catch (Exception e) { /* general catching...*/ }).

    Usually, you shouldn't catch an exception if you don't have something to do with it. Don't add a general catcher if you don't have a good reason.

    • Edited by Louis.fr Wednesday, January 2, 2013 10:54 AM
    Wednesday, January 2, 2013 10:52 AM