Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, September 28, 2010 4:43 PM
Hi,
Can i convert a Child Class instance to its base type ?
All replies (11)
Wednesday, September 29, 2010 10:05 AM ✅Answered
Are you sure you're using a class derived from Class4? I see you have defined a Class3 (with upper case C) and use a class3 (with lower case C).
Tuesday, September 28, 2010 5:02 PM
You can just treat it directly as a parent instance:
public class Parent {}
public class Child : Parent {}
Child child = new Child();
Parent childAsParent = child; // Just assign it to the base type directly.
Reed Copsey, Jr. - http://reedcopsey.com
Tuesday, September 28, 2010 5:36 PM
Please see my notes below:
class ParentClass
{
}
class ChildClass : ParentClass
{
}
class Test
{
public static void Execute()
{
// Create a parent and child
ParentClass parent = new ParentClass();
ChildClass child = new ChildClass();
// The next line works fine because child is ChildClass which is a descendent of ParentClass
ParentClass ref1 = child;
// An exception is thrown here because parent is not a ChildClass...it was created as ParentClass.
// You can't cast (force) an object to be something it is not.
ChildClass ref2 = (ChildClass)parent;
}
}
ShaneB
Tuesday, September 28, 2010 6:33 PM
Please see my notes below:
class ParentClass { } class ChildClass : ParentClass { } class Test { public static void Execute() { // Create a parent and child ParentClass parent = new ParentClass(); ChildClass child = new ChildClass(); // The next line works fine because child is ChildClass which is a descendent of ParentClass ParentClass ref1 = child; // An exception is thrown here because parent is not a ChildClass...it was created as ParentClass. // You can't cast (force) an object to be something it is not. ChildClass ref2 = (ChildClass)parent; } }ShaneB
Yes, that's correct.But why the same thing is giving error if i code it in another class's member function ?
Tuesday, September 28, 2010 6:39 PM
class Class5 { public void t() { Class4 obj=new Class4(); //obj base class3 obj1 = new class3(); //obj1 child Class4 ref1 = obj1; //error here-cannot implecetely convert ... Class3 ref2 = (Class3)obj; } } }
You're trying to go the other way, here.
When you assign obj1 to ref1, you're trying to assign a Class3 instance to a Class4 reference. This is trying to assign a base class instance to a derived class reference, which will fail.
Inheritance describes an "is-a" relation. In your case, Class4 is a type of Class3, which is why doing "Class3 ref2 = obj;" is valid. However, Class3 is not a type of Class4, so you can't do "ref1 = obj1;"
Reed Copsey, Jr. - http://reedcopsey.com
Tuesday, September 28, 2010 8:15 PM
Hi,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test.Test1
{
public class Vehicle { }
public class Bicycle : Vehicle { }
public class Car : Vehicle { }
class Program
{
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle();
Car carBis = new Car();
Bicycle bicycleBis = new Bicycle(); // bicycleBis "is-a" Bicycle
bicycleBis = carBis; // Wrong ! because carBis "is-not-a" Bicycle
bicycleBis = vehicle; // Wrong ! because vehicle "is-not-a" Bicycle
}
}
}
Regards,
Wednesday, September 29, 2010 1:13 AM
class Class5 { public void t() { Class4 obj=new Class4(); //obj base class3 obj1 = new class3(); //obj1 child Class4 ref1 = obj1; //error here-cannot implecetely convert ... Class3 ref2 = (Class3)obj; } } }
You're trying to go the other way, here.
When you assign obj1 to ref1, you're trying to assign a Class3 instance to a Class4 reference. This is trying to assign a base class instance to a derived class reference, which will fail.
Inheritance describes an "is-a" relation. In your case, Class4 is a type of Class3, which is why doing "Class3 ref2 = obj;" is valid. However, Class3 is not a type of Class4, so you can't do "ref1 = obj1;"
Reed Copsey, Jr. - http://reedcopsey.com
Class3 is Child class and Class4 is base class. I am trying to assign child class instance to base class reference(as opposed to what you are saying that i am trying to assign base class instance to a derived class reference).
As you said, "In your case, Class4 is a type of Class3, " is this correct ? Class4 is base class and Class3 derives it.So i think Class3 is a type of class4. Is'nt it?
Wednesday, September 29, 2010 2:41 PM
Are you sure that you're seeing the error on the line that you think?
When I put your code into VS 2008, I get the error as shown below:
Class4 obj = new Class4(); //obj base
Class3 obj1 = new Class3(); //obj1 child
Class4 ref1 = obj1;
Class3 ref2 = (Class3)obj; // Phil - I get the conversion error HERE
So I get the error on the FOURTH line, where you are trying to convert a Class4 object (obj) to a Class3 reference. In this case, you can't cast 'down' the inheritance tree, because obj is not a Class3. In fact, that's the term for what you're trying to do: downcast. It's very dangerous, actually. You should only attempt it if you are sure that it's the correct type. That's why the 'is' and the 'as' operators exist: for you to check that the type is what you need, to prevent the casting exception.
Phil
Wednesday, September 29, 2010 6:50 PM
Are you sure you're using a class derived from Class4? I see you have defined a Class3 (with upper case C) and use a class3 (with lower case C).
Oh!! really sorry.I was making that silly mistake.Now no error.But why VS was not showing error that i have taken wrong name.Since C# is purely case sensitive , why
class3 obj1 = new
class3(); //obj1 child
this line was not showing any error?
Since class3(lower case c) does not exist , why i was able to create instance of class3(lower case c)? Again great confusion?
What is meaning of Case Sensitive then here?
Thanks.
Thursday, September 30, 2010 8:32 AM
Hi bootstrap1,
Welcome to MSDN Forums!
You can use the following steps to find the reference and find the definition of class3 or the others.
1) Right click on the “Class4” (here, I will show you how to find the Class4 references not the class3, so you can instead of this when you want to find your class3 references )
2) Click on “Find All References”
3) After steps above, you can see the following information. You can double click the record to let the IDE help you locate the code line.
If there’s anything unclear, please feel free to let me know.
Have a nice day!
Mike
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.
*****************************************************
Sample world! You will get more from this world!
Welcome to the new world!
Friday, May 25, 2012 6:25 PM
Hi,
I have a similar problem - An exception type OpException that is defined outside my code which I need to serialize. The problem is OpExcpetion does a poor job of serialization and throws an exception during this.
To prevent this from happening I want to use only the properties from OpException that comes from Exception and I know serializaing this is going to be easy.
Any help here?
(Oh - and the people who wrote OpException have overriden GetBaseException so that it returns the same damn OpException. So I cant use that method )
Can I do something like this: (Exception)((Object)opex)) ???