|
-
April 15th, 2009, 03:02 AM
#1
[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,
-
April 15th, 2009, 03:39 AM
#2
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
-
April 15th, 2009, 03:41 AM
#3
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.
-
April 15th, 2009, 03:49 AM
#4
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.
-
April 15th, 2009, 04:00 AM
#5
Re: Questions about casting object types...
 Originally Posted by Mutant_Fruit
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?
-
April 15th, 2009, 04:04 AM
#6
Re: Questions about casting object types...
 Originally Posted by nabeelisnabeel
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!
-
April 15th, 2009, 04:07 AM
#7
Re: Questions about casting object types...
 Originally Posted by Mutant_Fruit
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!
-
April 15th, 2009, 04:19 AM
#8
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.
-
April 15th, 2009, 04:22 AM
#9
Re: Questions about casting object types...
 Originally Posted by Mutant_Fruit
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.
-
April 15th, 2009, 01:47 PM
#10
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'
}
-
April 16th, 2009, 02:47 AM
#11
Re: Questions about casting object types...
 Originally Posted by BigEd781
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|