Cloneable in Derived Classes - c#

Assume I have a class A, and B which derives from A:
class A : ICloneable
{
public object Clone() {...}
}
class B : A, ICloneable
{
public object Clone() {...}
}
which gives
'B.Clone()' hides inherited member 'A.Clone()'. Use the new keyword if hiding was intended.
warning.
(1) What is the suggested way? using new or declaring A.Clone() as virtual and override in B?
(2) If there are some members in A and properly cloned in A.Clone(), is there an easy way to clone them in B.Clone() or do I have to explicitly clone them in B.Clone() also?

If you have access to your source (which I'm guessing is the case here) then absolutely declare it as virtual and override it. If hide the base Clone with new might be a bad idea. If any code doesn't know that it's working with a B, then it will fire the wrong clone method and not return a proper clone.
Regarding the assignment of properties, perhaps consider implementing copy constructors and each level can handle its own cloning:
public class A : ICloneable
{
public int PropertyA { get; private set; }
public A()
{
}
protected A(A copy)
{
this.PropertyA = copy.PropertyA;
}
public virtual object Clone()
{
return new A(this);
}
}
public class B : A, ICloneable
{
public int PropertyB { get; private set; }
public B()
{
}
protected B(B copy)
: base(copy)
{
this.PropertyB = this.PropertyB;
}
public override object Clone()
{
return new B(this);
}
}
Each copy constructor calls the base copy constructor passing itself down the chain. Each inheritance level copies the properties belonging to it directly.
EDIT: If you use the new keyword to hide the base implementation, here's an example of what might happen. With a sample implementation (which on the face of it looks fine)
public class A : ICloneable
{
public int PropertyA { get; protected set; }
public object Clone()
{
Console.WriteLine("Clone A called");
A copy = new A();
copy.PropertyA = this.PropertyA;
return copy;
}
}
public class B : A, ICloneable
{
public int PropertyB { get; protected set; }
public new object Clone()
{
Console.WriteLine("Clone B called");
B copy = new B();
copy.PropertyA = this.PropertyA;
copy.PropertyB = this.PropertyB;
return copy;
}
}
But when you use it:
B b = new B();
A a = b;
B bCopy = (B)a.Clone();
//"Clone A called" Throws InvalidCastException! We have an A!

Related

How to Pass objects between classes

So what I'm trying to do here is pass the same copy of a class (class A) to another class (class B), but class B is instanced in class A.
Using a new statement in class B won't work because it would cause an infinite loop, as well as creating a new instance of it, when I want to be able to use variables from the 1st instance of class A.
I know about object.equals() but I can't use it until I define the class A's object in class B. Just using object.equals results in a null reference.
public partial class class_A : Form
{
public class_B _class_B = new class_B;
public Int32 var;
private void setclassA()
{
_class_B._class_A.equals(this);
}
}
public class class_B
{
public class_A _class_A; // I know this is null
// code
}
Like I said I want to avoid instancing a new copy of class A because I want the values in class A to be set.
I've tried using a method to do it but still get a null reference.
Pass A in the constructor of B:
public class A
{
private B _b;
public A()
{
_b = new B(this);
}
}
public class B
{
private A _a;
public B(A a)
{
_a = a;
}
}
As mentioned in the comments you're completely misunderstanding .Equals(). It's used to compare whether two objects are equal not clone / pass references.
Answer
You're 75% there. As Ron mentioned, just change .equals(this) to = this as follows:
_class_B._class_A = this;
Explanation
If we want to pass the same copy of ClassA to ClassB, and to do that when we instantiate ClassB inside of ClassA, then we use the this keyword, which represents the current instance of a class.
Though there are other ways to give this to ClassB, the below example shows two:
pass this to the constructor and do the property assignment in ClassB or
assign this directly to a property in ClassB.
The key for you is that = is an assignment operator whereas Equals() checks whether two variables refer to the same object or not.
Example
public class ClassA
{
public ClassB MyClassB1 { get; set; }
public ClassB MyClassB2 { get; set; }
public ClassA()
{
// pass `this` to the constructor
this.MyClassB1 = new ClassB(this);
// pass `this` directly to a property in `ClassB`
this.MyClassB2 = new ClassB();
this.MyClassB2.MyClassA = this;
}
}
public class ClassB
{
public ClassA MyClassA { get; set; }
public ClassB() { }
public ClassB(ClassA classA)
{
// do property assignment in the constructor
this.MyClassA = classA;
}
}
Proof of Concept
This runs as a Fiddle here and prints out "They are the same object," and some other things too in the more recent version.
using System;
public class Program
{
public static void Main()
{
var classA = new ClassA();
if(classA.Equals(classA.MyClassB1.MyClassA) &&
classA.Equals(classA.MyClassB2.MyClassA) &&
classA.MyClassB1.MyClassA.Equals(classA.MyClassB2.MyClassA))
{
Console.WriteLine("They are the same object.");
}
}
}
An important note is that, when we use this, we're giving ClassB a reference to ClassA not a copy of it. These are very different things.
Try this:
public class A
{
public B Instance_B;
public A(B b)
{
Instance_B = b;
}
}
public class B
{
public A Instance_A;
public B()
{
Instance_A = new A(this);
}
}
Use get; set;
In Class A:
public partial class class_A : Form
{
Class_B B = new Class_B();
B.Class_A = this;
public Int32 var;
}
Then in Class B:
public class class_B
{
Class_A A { get; set; }
// code
}

Implementing interface correctly in C# BOL classes

I've been searching for a bit and found similar things but nothing that quite answers my question. Here it is- I have two classes. A, B. I want class C to have all the properites of those two classes. This is in the Object Layer, c#.net.
Class A:
public class A
{
public string property1 { get; set; }
}
public class B
{
public string anotherproperty { get; set; }
}
public interface IB
{
B b { get; set; }
}
public class C : A, IB
{
//nothing here cause hopefully it inherits everything
}
I have no idea if my implementation is even close. I do get error message of "C does not implement interface member IB"
Anyone know how to do this?
I have two classes. A, B. I want class C to have all the properites of those two classes.
...then you will have to manually add either A's or B's properties to C. C# does not support multiple inheritance, sorry.
However, in most cases multiple inheritance is not needed. A common alternative is to use composition instead of inheritance: For example, you could have your class C provide references to instances of A and B:
public class C
{
private A a = new A();
private B b = new B();
public A A { get { return a; } }
public B B { get { return b; } }
}
This would allow you to access all properties via myC.A.property1 and myC.B.anotherproperty.
If you really need to combine implementations from both A and B in C, you might need to have a look at Mixin libraries for C#.
You can do something like this:
public class ClassA
{
public string AProp { get { return "AProp";} }
}
public class ClassB : ClassA
{
public string BProp { get { return "BProp"; } }
}
public class ClassC : ClassB
{
}
And then:
ClassC c = new ClassC();
Console.WriteLine(c.AProp);
Console.WriteLine(c.BProp);
Prints:
AProp
BProp
You can't override anything but you can access the inherited properties.

What actually happens when I perform a downcast?

How exactly does this work?
If I have this base class
public class BaseClass
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public BaseClass SimpleClone()
{
BaseClass result = new BaseClass();
result.Value1 = this.Value1;
result.Value2 = this.Value2;
return result;
}
}
And this child class
public class DerivedClass : BaseClass
{
public bool Value3 { get; set; }
public DerivedClass()
{
Value3 = true;
}
}
How can I downcast BaseCast.SimpleClone() into anotherObj? What would happen to Value3?
While knowing what happens is good, I am also interested in why it works this way.
If I understand correctly your question is What happens when you do the following
DerivedClass derived = (DerivedClass)baseObj.SimpleClone();
Did you try that? Simply it will result in InvalidCastException since BaseClass is not DerivedClass.
I have answered a similar question here, That should clear things up.
The question as it stands does not make sense. There is no possibility for downcasting, because your clone method already returns the base class. What I think you do (should) want here is override the clone method in your subclass:
public class DerivedClass : BaseClass
{
//... Other stuff
public BaseClass SimpleClone()
{
var result = new DerivedClass
{
Value1 = this.Value1,
Value2 = this.Value2,
Value3 = this.Value3,
}
return result;
}
Now, if you have an object of type DerivedClass and call clone on it, the compiler does not know that the returned object is of type DerivedClass, because of the method signature. But you do, so in that case you can cast the object. All it does is tell the compiler: 'I know better, you think this is a BaseClass but it actually is a DerivedClass'. It has no runtime impact because you don't change the type of the object, you only provide the compiler with extra information.
The answer to your downcast is no, it can't be done as per SriRam's answer. Although rather messy, you could achieve your cloning by using polymorphicism. You will probably need to split out the concerns of creation and assignment when doing so:
public class BaseClass
{
public int Value1 { get; set; }
public int Value2 { get; set; }
protected virtual BasedClass Create()
{
return new BaseClass();
}
public virtual BaseClass SimpleClone()
{
var clone = Create(); // The appropriate create will be called
clone.Value1 = this.Value1;
clone.Value2 = this.Value2;
return clone;
}
}
public class DerivedClass : BaseClass
{
public bool Value3 { get; set; }
public DerivedClass()
{
Value3 = true;
}
protected override BasedClass Create()
{
return new DerivedClass();
}
public override BaseClass SimpleClone()
{
var result = base.SimpleClone();
(result as DerivedClass).Value3 = this.Value3;
}
}
It doesn't work that way. Casting is way of telling the compiler to treat the object as if it was the object of casting class, and you are aware of information loss.
Just because a derived class is derived from base class doesn't necessarily mean that both are same.
So, you will get InvalidCastException.

Generic Methods passing subClass to baseClass defined Method?

QUESTION IS CLARIFIED
new thread : subclass properties accessed in generic method with superclass input
I have a base class A
subClasses B, C, and D inherit from A.
Each subClass has 3 subClasses a, b, and c.
a, b, and c have a list of unique properties.
However, now I want to build a generic function to access those properties, so how would I do this without switching on Type?
Clarification : I do not want a : B to have abstract C methods/properties
Example:
public void Method(A a){
if(a.isSubClassOf(B))
{Console.WriteLine(a.BaProperty);}
if(a.isSubClassOf(C))
{Console.WriteLine(a.CbProperty);}
if(a.isSubClassOf(D))
{Console.WriteLine(a.DcProperty);}
}
You can't define a member in derived class and access it via the reference to base class without casting to derived class:
class A {}
class B
{
public int i;
}
A a = new B();
a.i = 0; // error
((B)a).i = 0; // OK
Either declare virtual property in any of base types in your hierarchy, or use casting to the concrete derived type. Of course, in the second case your method doesn't make any sense.
Typically, you would use a virtual or abstract method defined in A and overridden in the subclasses.
public abstract class A
{
protected abstract PropertyType PropertyValue {get;}
public void Method()
{
Console.WriteLine(PropertyValue);
}
}
public class B : A
{
protected override PropertyType Property { get { return PropertyType.B; } }
}
// etc...
An elegant solution is to override ToString
public abstract class A { }
public class B : A {
public int b { get; set; }
public override string ToString()
{
return b.ToString();
}
}
// Do the same with C and D ....
A[] array = new A[] { new B(), new C(), new D() };
foreach (A a in array) {
Console.WriteLine(a);
}
Note that Console.WriteLine does not need to know about a special method or property in A. It also works for types not deriving from A.
It depends greatly in what you really want to achieve. In some cases what Steve Czetty suggests is the best option.
In others you could just keep all the properties different and have a virtual method in the base class that returns for example in this case a "string" that you can then write in the console or anything you wish.
Edit: You could override ToString as Olivier suggested. But only if you feel what yo are going to retun is the "String representation of the object".
public abstract class A
{
public string PropertyA { get; set; }
public virtual string GetString() //
{
return PropertyA;
}
}
public class B:A
{
public string PropertyB { get; set; }
public override string GetString()
{
return PropertyB;
}
}
public class C:A
{
public string PropertyC { get; set; }
public override string GetString()
{
return string.Format("{0} - {1}", base.GetString(), PropertyC) // for example if you wanted to do something more complex
}
}
Now if what you need can not be solved like this, you could cast as Dennis Suggested.
There is another posibility: you could use the visitor pattern. Here you can find several ways to implement it.
Just so you have an idea you would wind up having a class similar to this: (it will depend on what you really want to achieve)
You have to implement some other basic things (interface and some methods), but from a Reference to the base class you will be able to call the corresponding "Visit" method easily. There is a lot of detail in the link i added.
class ClassicVisitor : IAVisitor
{
public string SuperComplexStringResult { get;set; }
public void Visit(B b) { SuperComplexStringResult = String.Format("Super Complex Stuff + {0}", b.PropertyB); }
public void Visit(C c) { SuperComplexStringResult = String.Format("Super Complex Stuff + {0}", c.PropertyC); }
}

How do I create clone of object in base class?

I need a method that creates an empty clone of an object in a base class? For instance:
public class ChildClass : ParentClass
{
public ChildClass()
{
}
}
public class ParentClass
{
public SomeMethod()
{
// I want to create an instance of the ChildClass here
}
}
Up until now, we have an abstract method defined in the parent class. And, all of the child classes implement them. But, the implementation is the same for all, just a different type.
public class ChildClass : ParentClass
{
public ChildClass()
{
}
public ParentClass CreateEmpty()
{
return new ChildClass();
}
}
public class ParentClass
{
public SomeMethod()
{
// I want to create an instance of the ChildClass here
ParentClass empty = CreateEmpty();
}
public abstract ParentClass CreateEmpty();
}
Is there any way to do this from the parent class so that I don't have to keep implementing the same logic for each different child class? Note that there may be more levels of inheritance (i.e. ChildChildClass : ChildClass : ParentClass).
If using reflection isn't a problem to you, you could do it using Activator class:
//In parent class
public ParentClass CreateEmpty()
{
return (ParentClass)Activator.CreateInstance(this.GetType());
}
This will return empty object of the type you want. Notice that this method does not need to be virtual.
On the other hand, I think that your current approach is perfectly fine, few more lines of code aren't so bad.
You can make a deep clone of the object using the binary serializer.
EDIT: Just noticed the word "empty" next to clone (which I thought was an oxymoron). Leaving this response up anyhow hoping it will help others that find this question because they are looking to do a regular clone.
This is somewhat experimental. I don't know whether this will lead to a cyclic dependency. Haven't touched C# for some months.
public class ParentClass<T> where T : ParentClass<T>, new() { // fixed
public ParentClass() {
var x = new T(); // fixed, was T.new()
}
}
public class ChildClass : ParentClass<ChildClass> {
public ChildClass() { }
}
Otherwise go for the ReflectionCode by Ravadre.
I'm using the following pattern.
Pros:
This pattern secure the type-safety of cloning in private and public sides of classes.
The output class will be always correct.
You never forgot override the "clone" method. The "MyDerivedClass" never returns another class than the "MyDerivedClass".
Cons:
For one class, you need create one interface and two classes (prototype and final)
Sample:
// Common interface for cloneable classes.
public interface IPrototype : ICloneable {
new IPrototype Clone();
}
// Generic interface for cloneable classes.
// The 'TFinal' is finaly class (type) which should be cloned.
public interface IPrototype<TFinal> where TFinal : IPrototype<TFinal> {
new TFinal Clone();
}
// Base class for cloneable classes.
// The 'TFinal' is finaly class (type) which should be cloned.
public abstract class PrototypeBase<TFinal> : IPrototype<TFinal> where TFinal : PrototypeBase<TFinal> {
public TFinal Clone() {
TFinal ret = this.CreateCloneInstance();
if ( null == ret ) {
throw new InvalidOperationException( "Clone instance was not created." );
}
this.FillCloneInstance( ret );
return ret;
}
// If overriden, creates new cloned instance
protected abstract TFinal CreateCloneInstance();
// If overriden, fill clone instance with correct values.
protected abstract void FillCloneInstance( TFinal clone );
IPrototype IPrototype.Clone() { return this.Clone(); }
object ICloneable.Clone() { return this.Clone(); }
}
// Common interface for standalone class.
public interface IMyStandaloneClass : IPrototype<IMyStandaloneClass> {
string SomeText{get;set;}
string SomeNumber{get;set;}
}
// The prototype class contains all functionality exception the clone instance creation.
public abstract class MyStandaloneClassPrototype<TFinal> : PrototypeBase<TFinal>, IMyStandaloneClass where TFinal : MyStandaloneClassPrototype<TFinal> {
public string SomeText {get; set;}
public int SomeNumber {get; set}
protected override FillCloneInstance( TFinal clone ) {
// Now fill clone with values
clone.SomeText = this.SomeText;
clone.SomeNumber = this.SomeNumber;
}
}
// The sealed clas contains only functionality for clone instance creation.
public sealed class MyStandaloneClass : MyStandaloneClassPrototype<MyStandaloneClass> {
protected override MyStandaloneClass CreateCloneInstance() {
return new MyStandaloneClass();
}
}
public interface IMyExtendedStandaloneClass : IMyStandaloneClass, IPrototype<IMyExtendedStandaloneClass> {
DateTime SomeTime {get; set;}
}
// The extended prototype of MyStandaloneClassPrototype<TFinal>.
public abstract class MyExtendedStandaloneClassPrototype<TFinal> : MyStandaloneClassPrototype<TFinal> where TFinal : MyExtendedStandaloneClassPrototype<TFinal> {
public DateTime SomeTime {get; set;}
protected override FillCloneInstance( TFinal clone ) {
// at first, fill the base class members
base.FillCloneInstance( clone );
// Now fill clone with values
clone.SomeTime = this.SomeTime;
}
}
public sealed class MyExtendedStandaloneClass : MyExtendedStandaloneClassPrototype<TFinal> {
protected override MyExtendedStandaloneClass CreateCloneInstance() {
return new MyExtendedStandaloneClass
}
}

Categories

Resources