CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Casting...

Threaded View

  1. #5
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Casting...

    Quote Originally Posted by Arjay
    As personal preference, I generally try to avoid casting as much as possible in my code. As such I rarely pass objects to and from methods.
    How do you manage this in C# where everything derives from System.Object lol.

    Also, C# has a nice little sugar syntax tweak that allows you to do this:

    Code:
    enum Color { Red, Green, Blue };
    
    Color color =  Enum.Parse( typeof( Color ), "Green" ) as Color;
    
    // The value of color is 'Color.Green'.
    Notice the "as Color" syntax on the end. I never use it, Im more partial to the C++ way but some developers might find it handy.

    EDIT:

    Oops, I forgot color was a VALUE type, therefore casting from a reference type to a value type does NOT work with the "as" syntax sugar. However, any reference type to reference type casts will work with it.
    Last edited by RaleTheBlade; July 21st, 2008 at 07:30 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured