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

Thread: Casting...

  1. #1
    Join Date
    Apr 2008
    Posts
    61

    Casting...

    Hi there,

    I'm in the throws of learning C#, but I am currently trying to figure out why I would use casting with reference types. From what I understand, casting is used to convert one type to another (please correct me if I am wrong), and I understand this concept when using primitive data types. What I am struggling to understand is why you would use casting when dealing with reference types.

    I've checked various sites, but none of them offer a clear understanding of why you would cast one object to another.

    Am I missing something fundamental to OOP?

    Any help would be gratefully received.

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: Casting...

    Well, the variable (in C#, 99 % of the variables are a "pointer" to a referenced object) is of a specific object.

    In C++, you also cast a pointer to a specific type if you want to call its functions. In C#, it works exactly the same. You need to convert the object to the right type so you can call the right functions on it.

  3. #3
    Join Date
    Apr 2008
    Posts
    61

    Re: Casting...

    Quote Originally Posted by Tischnoetentoet
    Well, the variable (in C#, 99 % of the variables are a "pointer" to a referenced object) is of a specific object.

    In C++, you also cast a pointer to a specific type if you want to call its functions. In C#, it works exactly the same. You need to convert the object to the right type so you can call the right functions on it.
    Many thanks for your response.

    Do you have any recommended web-sources that give a thorough explanation?

    Again, thanks for your help.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Casting...

    All types in C# derive from object. Some methods operate in a generic fashion and return an object that must be cast to the desired type.

    For example, consider the System.Enum.Parse method which returns an object. Given a string and an enum type, this method returns an object that can be cast to the enum type (with the string represented as an enum value).

    Code:
    enum Color { Red, Green, Blue };
    
    Color color = (Color) Enum.Parse( typeof( Color ), "Green" );
    
    // The value of color is 'Color.Green'.
    In the above snippet, you would get an 'unable to convert type' error if you tried the following without the cast:
    Code:
    Color color = Enum.Parse( typeof( Color ), "Green" );
    Read more in Casting (C# Programming Guide).

    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.

  5. #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

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Casting...

    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.
    Quote Originally Posted by RaleTheBlade
    How do you manage this in C# where everything derives from System.Object lol.
    Because I avoid passing objects in my methods.

    For example, I tried to avoid doing this:
    Code:
    void Method( object obj );
    If a method or class can operate on multiple types, I'll use generics.

    Passing raw objects is always a last resort (just like I didn't like to pass void* in C++).

  7. #7
    Join Date
    Apr 2008
    Posts
    61

    Re: Casting...

    Thanks for the responses, guys. Very much appreciated!

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