Writing an asp.net mvc app and i have something like this...
Public Class AA
'... has some variables...
End Class
Public Class BB
Inherits AA
Public ExtraVariable As Integer ' just adds another variable and thats it!
End Class
So, now in m开发者_运维技巧y program, i just want to copy object of type AA to an empty variable of type BB?
it makes sense to do this, as i expect all the fields in AA type object to be copied to the newly created BB type object, and the ExtraVariable in the BB type object i would (will) just assign a value to it later (after the copy) on my own time!!
I know copying BB type to AA type would not be appropriate as there would be lost data!
But im trying to copy AA to BB, I've used both DirectCast
and CType
to do this, and i keep getting "unable to cast" error!!!
Note: I'm using vb.net (but can read c#, no problems)
It seems like you're confusing copying with simple assignment.
What you likely want to do is define a constructor on BB that takes an AA as an argument, and copies the values.
As anon says you probably want to pass AA into BB's constructor, where you can copy all the elements:
public class AA
{
//some variables
}
public class BB : AA
{
public BB(AA aa)
{
//Set BBs variables to those in AA
someVariable= aa.someVariable
}
public int SomeExtraProperty{get;set;}
}
but obviously you'll have any inherited constructors available to you which you could reuse if appropriate.
EDIT
based on some of the comments above, you could also do:
public class BB : AA
{
private AA _aa;
public BB(AA aa)
{
//Set BBs variables to those in AA
_aa=aa;
}
public int SomeExtraProperty{get;set;}
//override inherted members and just delegate to the internal object
public override int SomeMethod()
{
return _aa.SomeMethod();
}
}
or maybe go for the Decorator Pattern
I do not think what you are trying is possible. A reference to a derived class must actually refer to an instance of the derived class
see: Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.
Here is one way to get the values
public class AA
{
public string name;
}
public class BB : AA
{
public BB(AA aa)
{
name = aa.name;
}
}
It seems that you misunderstood the inheritance concept correctly.
When you copy BB to AA you don't miss any data, as you can get the data if you cast it again to BB.
AA cannot be cast to BB directly, because AA is not BB. If you want, you can do a custom cast and you can specify how the cast will be.
But, doing this it not will be very intuitive, and is better to put the value "by hand":
bb.Value1 = aa.Value1 bb.Value2 = aa.Value2 bb.Value3 = aa.Value3
Nothing wrong about it.
There are some good posts on object cloning you may want to have a look at, for example Deep cloning objects, but what people have already mentioned here about manually copying is probably your best bet in copying from one Type to another.
Your problem seems to in your understanding of inheritance and casting. Something to bare in mind when casting is that you can easily cast down to a simpler type, but vice versa is a lot trickier, and not possible with a simple cast
ie
with the classes:
public class BaseObject
{
}
public class InheritedObject : BaseObject
{
}
public class BaseObjectConsumer()
{
Consumer(BaseObject base);
}
public class InheritedObjectConsumer()
{
}
The following should be considered:
InheritedObject io = new InheritedObject();
BaseObject bo = new BaseObject();
BaseObjectConsumer(io); //this is possible as the InheritedObject is being 'viewed' as a BaseObject
InheritedObjectConsumer(bo); //Not as simple, as the Base object is not necessarily an Inherited object, so we can't cast 'up'
精彩评论