CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2008
    Posts
    61

    [RESOLVED] Questions about casting object types...

    Hi there, ladies and gents.

    I am still in the full throws of learning C# and OOP; however, I'm not actively using it on a day-to-day basis, so my knowledge is based on books rather than real-world programming.

    I have a concept I feel I understand (I hope) - casting - but I wanted to reaffirm my understanding with you experienced C#/OOP programmers to make sure I am on the right track. I'd be very grateful if I could get your input.

    Please forgive my explanation; I hope it is easy to understand.

    Question 1:

    Consider the following statement:

    Code:
    Object Obj = new Employee();
    As I understand it, the above statement declares a reference variable of type Object, which is assigned a memory address pointing to an Employee object created by the new keyword. Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?

    Questions 2:

    Consider the following statement:

    Code:
    Employee emp = Obj;
    Am I correct in thinking that the above statement would cause an invalid casting exception error given this attempted implicit cast?

    And to rectify this, I'd need to perform an explicit cast?

    Code:
    Employee emp = (Employee)Obj;
    Does the above cast cause both reference variables emp and Obj to point to the same object? Would their 'view' of the object differ based on their declared types?

    Given the explicit cast, can emp now see the full interface details defined in the Employee class?

    Many thanks in advance for your help,

  2. #2
    Join Date
    Apr 2006
    Posts
    220

    Re: Questions about casting object types...

    Question 1:
    Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?

    Yes you are are correct. Obj reference cannot see the implementation details of the Employee object unless you typecast it to Employee.

    For example you Employee class is

    Code:
    class Employee
    {
        public string EmpName
        {
            return "MyName";
        }
    }
    Then , take a look here

    Code:
    Object Obj = new Employee();
    print(Obj.EmpName);             // Compiler Error here because obj cannot see EmpName yet
    print(((Employee)Obj).EmpName);             // Works fine

  3. #3
    Join Date
    Apr 2006
    Posts
    220

    Re: Questions about casting object types...

    Questions 2:

    Consider the following statement:

    Code:
    Employee emp = Obj;
    Am I correct in thinking that the above statement would cause an invalid casting exception error given this attempted implicit cast?

    Definitely, the compiler will give Invalid Cast error. You will have to explicitly type cast it.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Questions about casting object types...

    Code:
    Employee emp = (Employee)Obj;
    -- Edit --
    I meant to quote the following:
    object o = new Employee ();
    Employee e = o;
    -- End edit --

    An exception won't be thrown, it'll be a compile time error. However, if you wrote:

    Code:
    int a = 5;
    object o = a;
    Employee employee = (Employee) o;
    That would compile, but you'd get a runtime error (invalid cast exception).
    Last edited by Mutant_Fruit; April 15th, 2009 at 04:20 AM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Apr 2008
    Posts
    61

    Re: Questions about casting object types...

    Quote Originally Posted by Mutant_Fruit View Post
    Code:
    Employee emp = (Employee)Obj;
    An exception won't be thrown, it'll be a compile time error.
    However, if you wrote:

    Code:
    int a = 5;
    object o = a;
    Employee employee = (Employee) o;
    That would compile, but you'd get a runtime error (invalid cast exception).




    I thought this explicit cast

    Code:
    Employee emp = (Employee)Obj;
    would work given the Obj reference variable points to an Employee object?

  6. #6
    Join Date
    Apr 2008
    Posts
    61

    Re: Questions about casting object types...

    Quote Originally Posted by nabeelisnabeel View Post
    Question 1:
    Am I correct in thinking that the the Obj reference variable cannot see the implementation details of the Employee object, only those defined by Object?

    Yes you are are correct. Obj reference cannot see the implementation details of the Employee object unless you typecast it to Employee.

    For example you Employee class is

    Code:
    class Employee
    {
        public string EmpName
        {
            return "MyName";
        }
    }
    Then , take a look here

    Code:
    Object Obj = new Employee();
    print(Obj.EmpName);             // Compiler Error here because obj cannot see EmpName yet
    print(((Employee)Obj).EmpName);             // Works fine
    Thanks for the response!

  7. #7
    Join Date
    Apr 2008
    Posts
    61

    Re: Questions about casting object types...

    Quote Originally Posted by Mutant_Fruit View Post
    Code:
    Employee emp = (Employee)Obj;
    An exception won't be thrown, it'll be a compile time error. However, if you wrote:

    Code:
    int a = 5;
    object o = a;
    Employee employee = (Employee) o;
    That would compile, but you'd get a runtime error (invalid cast exception).

    Thanks for the response!

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: Questions about casting object types...

    Whoops, i quoted the wrong thing. I meant to say:

    Code:
    object o = new Employee ();
    Employee e = o;
    That's a compile time error as object is not a subclass of Employee.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Apr 2008
    Posts
    61

    Re: Questions about casting object types...

    Quote Originally Posted by Mutant_Fruit View Post
    Whoops, i quoted the wrong thing. I meant to say:

    Code:
    object o = new Employee ();
    Employee e = o;
    That's a compile time error as object is not a subclass of Employee.
    So this is a valid statement and won't cause an exception at runtime?

    Code:
    Employee emp = (Employee)Obj;


    Thanks again for your help.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Questions about casting object types...

    Well, it may cause an exception at runtime of 'Obj' is not an Employee object under the hood. You would be better served using a safe cast on reference types:

    Code:
    object o = new Employee();
    // some time later
    Employee e = o as Employee;
    if ( e != null )
    {
        // the cast succeeded
    }
    else
    {
        // the cast failed, 'o' is not of the type 'Employee'
    }

  11. #11
    Join Date
    Apr 2008
    Posts
    61

    Re: Questions about casting object types...

    Quote Originally Posted by BigEd781 View Post
    Well, it may cause an exception at runtime of 'Obj' is not an Employee object under the hood. You would be better served using a safe cast on reference types:

    Code:
    object o = new Employee();
    // some time later
    Employee e = o as Employee;
    if ( e != null )
    {
        // the cast succeeded
    }
    else
    {
        // the cast failed, 'o' is not of the type 'Employee'
    }
    Understood. Thanks.

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