Click here to Skip to main content
15,891,431 members
Home / Discussions / C#
   

C#

 
GeneralRe: Control Designer & Child Controls Issue Pin
Heath Stewart23-Sep-03 6:47
protectorHeath Stewart23-Sep-03 6:47 
GeneralRe: Control Designer & Child Controls Issue Pin
scott@otech.com24-Sep-03 7:51
scott@otech.com24-Sep-03 7:51 
GeneralRe: Control Designer & Child Controls Issue Pin
Heath Stewart24-Sep-03 8:50
protectorHeath Stewart24-Sep-03 8:50 
GeneralRe: Control Designer & Child Controls Issue Pin
scott@otech.com24-Sep-03 9:02
scott@otech.com24-Sep-03 9:02 
Questioncopy constructor needed? Pin
berndg23-Sep-03 2:59
berndg23-Sep-03 2:59 
AnswerRe: copy constructor needed? Pin
Heath Stewart23-Sep-03 3:12
protectorHeath Stewart23-Sep-03 3:12 
GeneralRe: copy constructor needed? Pin
Alvaro Mendez23-Sep-03 4:43
Alvaro Mendez23-Sep-03 4:43 
GeneralRe: copy constructor needed? Pin
Heath Stewart23-Sep-03 5:39
protectorHeath Stewart23-Sep-03 5:39 
There's nothing wrong with either method, I just prefer implementing ICloneable because it's consistent with everything else in .NET and "Microsoft's way of doing things". Poke tongue | ;-P

You can actually eliminate the need for a cast by having your own Clone() method and implementing ICloneable explicitly:
public class MyClass : ICloneable
{
  // ...
  public MyClass Clone()
  {
    // Create the clone and return.
  }
  object ICloneable.Clone()
  {
    return this.Clone();
  }
}
Explicit interfaces are great in many cases, especially when you have to define a method that is defined in an interface already, or implementing two or more interfaces that may have the same members. They also come in handy for typed expressions like above. Several of your ICollection implementations in the BCL do this very thing to, at the very least, give you the appearance of a typed return and to prevent you from having to cast.

As I said, though, you could use both ways::
public class MyClass : ICloneable
{
  // ...
  public MyClass(MyClass orig)
  {
    // Create this clone from the original.
  }
  public MyClass Clone()
  {
    return new MyClass(this);
  }
  object ICloneable.Clone()
  {
    return this.Clone();
  }
}
You get the best of both worlds, then.

Another reason, which I use in a complex and abstract tree "framework" in the app I architected for work, is for treating object instances depending on what they implement. While recursing through a tree during various operations (such as cloning a tree) I determine if the object (so remember, I have no idea if a copy constructor exists for the Type without using quite a bit of reflection and many extra calls) implements ICloneable. If it does, I clone it. If it doesn't, I use a tab bit of reflection to create a new instance and do a memberwise clone (shallow copy). Most of the objects in the tree implement ICloneable, so I don't revert to reflection often and will most likely remove that functionality in a future version. The tree objects facilitate cloning by checking certain member fields for cloneability and do things in a similar matter. This creates a large chunck of functionality that never has to deal with any specific types in either the base class or the various child classes for a very nice OO design.

This is just one example where using the interface helps, but it's not required. It just depends on circumstances (checking for a copy constructor for the conditions above would be more expensive) and personal preferences (like I said, I like to remain consistent with most aspects of whatever API/framework I'm writing with/for).

Because it is so easy to do (see the second code example above), I would recommend doing both the copy ctor and implement ICloneable if you do, indeed, want to include a copy ctor. The extra IL would be trivial / negligable and it would allow for better flexibility.

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: copy constructor needed? Pin
Alvaro Mendez23-Sep-03 10:37
Alvaro Mendez23-Sep-03 10:37 
GeneralRe: copy constructor needed? Pin
Heath Stewart23-Sep-03 10:47
protectorHeath Stewart23-Sep-03 10:47 
GeneralRe: copy constructor needed? Pin
Alvaro Mendez23-Sep-03 11:01
Alvaro Mendez23-Sep-03 11:01 
GeneralRe: copy constructor needed? Pin
Heath Stewart23-Sep-03 13:04
protectorHeath Stewart23-Sep-03 13:04 
GeneralWebservice over SSL with multiple servers Pin
solidstore23-Sep-03 1:14
solidstore23-Sep-03 1:14 
GeneralRe: Webservice over SSL with multiple servers Pin
Heath Stewart23-Sep-03 3:25
protectorHeath Stewart23-Sep-03 3:25 
GeneralRe: Webservice over SSL with multiple servers Pin
solidstore23-Sep-03 3:54
solidstore23-Sep-03 3:54 
GeneralRe: Webservice over SSL with multiple servers Pin
Heath Stewart23-Sep-03 4:34
protectorHeath Stewart23-Sep-03 4:34 
GeneralRe: Webservice over SSL with multiple servers Pin
Heath Stewart23-Sep-03 4:36
protectorHeath Stewart23-Sep-03 4:36 
GeneralNamed Mutex (I think) Pin
James Simpson23-Sep-03 0:52
James Simpson23-Sep-03 0:52 
GeneralRe: Named Mutex (I think) Pin
Heath Stewart23-Sep-03 3:31
protectorHeath Stewart23-Sep-03 3:31 
GeneralRe: Named Mutex (I think) Pin
Blake Coverett23-Sep-03 6:14
Blake Coverett23-Sep-03 6:14 
Generalaspnet_wp.exe restarting unexpectedly Pin
solidstore22-Sep-03 23:52
solidstore22-Sep-03 23:52 
GeneralOutlook Automation and sending Bulk E-mail Pin
Braulio Dez22-Sep-03 23:44
Braulio Dez22-Sep-03 23:44 
GeneralTheoretical Question on object creation Pin
Rohde22-Sep-03 23:09
Rohde22-Sep-03 23:09 
GeneralRe: Theoretical Question on object creation Pin
shaunAustin22-Sep-03 23:16
shaunAustin22-Sep-03 23:16 
GeneralRe: Theoretical Question on object creation Pin
Rohde22-Sep-03 23:30
Rohde22-Sep-03 23:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.