3

It is possible to cast class1 to class2 ? result variable retrieves always null value.

class Program { static void Main(string[] args) { Class1 class1 = new Class1(); class1.Field1 = "1"; class1.Field2 = "2"; class1.Field3 = "3"; class1.Field4 = "4"; class1.Field5 = "5"; BaseClass base1 = (BaseClass)class1; Class2 class2 = base1 as Class2; string result = class2.Field1; } } public class BaseClass { public string Field1 { get; set; } public string Field2 { get; set; } public string Field3 { get; set; } } public class Class1 : BaseClass { public string Field4 { get; set; } public string Field5 { get; set; } } public class Class2 : BaseClass { public string Field6 { get; set; } public string Field7 { get; set; } } 
2
  • I think he wants "Class2 class2 = base1 as Class2" to work. Commented Mar 7, 2011 at 10:02
  • 1
    For this feature, I would implement a constructor that takes an existing BaseClass instance and copies values from it. Casting using a custom operator would be confusing. Commented Mar 7, 2011 at 10:23

6 Answers 6

5

No, it's not possible to cast like this. You'll need to write your own conversion method or operator. The object base1 refers to simply isn't a Class2. What would its values of Field6 and Field7 be?

Sign up to request clarification or add additional context in comments.

4 Comments

I am expecting to have new Class2 instence with Field1, Field2 and Field3 values. Field6 and Field7 can be null because they are not in base class.
Change Class1, Class2, BaseClass with real world examples, and you'll see why that does not make sense.
@Cihan: Then you'll need to create a Class2 constructor which takes a BaseClass parameter and copies the values from that into the new object.
that's exactly what I said (commented) 2 minutes ago :-P. Also +1 for the answer.
0

It's not possible to cast Class1 to Class2. But to BaseClass is, of course, possible.

Comments

0

"Can it be done?"

No, they are both BaseClass and can both be cast to BaseClass, but you cannot cast Class1 to Class2 or vice versa. Nor can BaseClass be cast to Class1. You can only cast upwards in the inheritance chain.

If you are trying to overcome cumbersome mappings or something, consider AutoMapper for your mappings which is really helpful in those cases.

Comments

0

The problem here is that your Class1 cannot be casted as Class2.

When you do

BaseClass base1 = (BaseClass)class1; 

You are only changing the reference and not the object itself. Object knows that it is a Class1. So when you try to cast as Class2, it fails since one is not inherited from the other.

Comments

0

The problem line is:

Class2 class2 = base1 as Class2; 

The problem is that this would have no idea what to do with Field6 or Field7. You have to write your own conversion so that these fields get sorted out.

Comments

0

Assuming that the question is "Why does string result = class2.Field1; die with a NullReferenceException" I'll try to explain.

Lets rename the classes a bit. Lets say BaseClass is Vehicle, Class1 is Car, and Class2 is SpaceShuttle.

So,

BaseClass base1 = (BaseClass)class1; //becomes Vehicle myVehicle = (Vehicle) myCar; 

No problem there, the cast is not even neccessary. A car is always a vehicle. But

Class2 class2 = base1 as Class2; //becomes SpaceShuttle shuttle = myVehicle as SpaceShuttle. 

Here i'm saying that my vehicle is a spaceShuttle, when in fact is an old Honda Civic. The compiler notices this, and sets the shuttle variable to null, because thats what the as operator does. If you ask myVehicle is it a SpaceShuttle, it will respond with a NO, like this:

bool isShuttle = (myVehicle is SpaceShuttle); //false 

So even if both SpaceShuttle and Car have the same property, inherited from Vehicles, lets say Color or Owner or whatever, it does not mean that I can treat my car as a space shuttle.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.