Re: Implementing interface problem
From: Kurt (kurbylogic_at_hotmail.com)
Date: 11/22/04
- Next message: Joel Martinez: "Re: Play MP3 file via C#"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: How good an encryption algorithm is this?"
- In reply to: hex: "Implementing interface problem"
- Next in thread: Jeff Louie: "Re: Implementing interface problem"
- Reply: Jeff Louie: "Re: Implementing interface problem"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Joel Martinez: "Re: Play MP3 file via C#"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: How good an encryption algorithm is this?"
- In reply to: hex: "Implementing interface problem"
- Next in thread: Jeff Louie: "Re: Implementing interface problem"
- Reply: Jeff Louie: "Re: Implementing interface problem"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|