Create a Type-Safe Clone Method
Problem
You want to create a Clone method that returns an object of the correct type. However, the ICloneable.Clone method always returns a generic object.
Solution
Make the method that implements ICloneable.Clone private, and add another, strongly typed Clone method.
Discussion
The ICloneable interface always returns a generic System.Object reference, which means that the client must use casting code to convert the object to the appropriate type. However, you can remove this extra step by adding a strongly typed Clone method, as shown here.
Public Class Person Implements ICloneable Private _FirstName As String Private _LastName As String ' (Property procedure and constructor code omitted.) Private Function CloneMe() ...
Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.