Re: Implementing interface problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Kurt (kurbylogic_at_hotmail.com)
Date: 11/22/04


Date: 22 Nov 2004 12:17:18 -0800

hex <hex@discussions.microsoft.com> wrote in message news:<341D78B7-15FF-495E-9B44-B867CEA41B11@microsoft.com>...
> Hi
>
> I make a class "MyClass" and this clas implements the Interface ICloneable.
> I want when I instance an object from MyClass and I call obj.Clone() it
> returns an object of MyClass type.
>
> for example:
> public class MyClasss : ICloneable
> {
> public void A();
> public object Clone(){} // The ICloneable Method
> }
>
> when I do this:
> MyClass obj;
> obj.Clone() I want also call the method A. but Clone returns an object, not
> a MyClass object. If I defines the Clone method like this: public MyClass
> Clone{} I receive an error, It is logic. but all .NET classes that implements
> the interface ICloneable, the method Clone returns an object of the same type
> of the class that implement this interface.
>
> how can I do this!
>
> thanks

public class Foo : ICloneable
{
  public Foo Clone() { return CloneFoo(); }
  object ICloneable.Clone() { return CloneFoo(); }
  protected virtual Foo CloneFoo() { return new Foo(); }
}
For decendants of foo you need can use the new keyword on the method
to hide the base class method and return a different type. The new
method is only invoked when the caller is using the Foo2 interface as
opposed to Foo interface or clonable interface so normally the use of
the new keyword is strongly discurraged however in this case the
functionality is exactly the same only the signature is changed and
therefore is required.

public class Foo2 : Foo
{
  protected override Foo CloneFoo() { return new Foo2(); }
  public new Foo2 Clone() { return (Foo2)CloneFoo(); }
}

- Kurt



Relevant Pages

  • Re: Implementing interface problem
    ... What I want is that my class implements some interface, ... the Clone method, but this method does not returns an object as ICloneable ... not a MyClass object.” I want MyClass.Clonereturns a MyClass and ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Implementing interface problem
    ... public class Foo: ICloneable ... public Foo Clone() ... object ICloneable.Clone{return CloneFoo();} ... method is only invoked when the caller is using the Foo2 interface as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Module inclusion and class macro problem
    ... MyClass but in the separate Interface::xxx modules, ... limiting yourself to just one interface when you are using inheritage. ... def enforce_interface ...
    (comp.lang.ruby)
  • Re: ClassLoader not loading recompiled classes
    ... MyClass mc=klass.newInstance; ... public class MyClass ... java Main ... and then another from another class loader. ...
    (comp.lang.java.programmer)
  • Re: empty interfaces via reflection
    ... Have a custom class loader since the system ... MyClass ... public class MyClassLoader extends ClassLoader ...
    (comp.lang.java.programmer)