471,349 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

ICloneable

In VB 2005, the ICloneable interface requires the following:

Class foo
Implements ICloneable

Public Function Clone() as Object Implements System.ICloneable.Clone
' return new_object of type foo
End Function

End Class

However, with Option Strict On, to use this requires

newfoo as foo = CType(oldFoo.Clone(), foo)

This takes additional CPU cycles at runtime - first to coerce foo's clone to
object and then to coerce the clone back to foo.

In the case of a non-inheritable object with a Clone function, is there any
reason to use:

Class foo

Public Function Clone() as foo
' return new_object of type foo
End Function

End Class

Now you can say

option strict on

newfoo = oldFoo.Clone()

Thanks,
Mike Ober.


Jul 21 '05 #1
1 2587
It seems like you just explained the reason to use a strongly typed
version (to avoid the casting to/from object). Did you mean "is there
any reason NOT to use:" ?

The only reason I would say not to use the strongly typed version (as
foo) is if other objects expect your class to implement ICloneable. If
you need to implement ICloneable, you will need to create a Clone() that
returns object. In C# you could have both by explicitly implementing
ICloneable.Clone() - I'm not positive how you would do it in VB.NET. I
think you would have to rename your strongly typed Clone() to something
else, like FooClone().
In the case of a non-inheritable object with a Clone function, is there any
reason to use:

Class foo

Public Function Clone() as foo
' return new_object of type foo
End Function

End Class

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by smith | last post: by
6 posts views Thread by Andy | last post: by
2 posts views Thread by Venkat Venkataramanan | last post: by
7 posts views Thread by Don | last post: by
1 post views Thread by Michael D. Ober | last post: by
1 post views Thread by Rain | last post: by
2 posts views Thread by Nathan | last post: by
6 posts views Thread by Stefan Hoffmann | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.