Implementation of the ICIoneable interface

The following code example shows the implementation of the ICloneable interface in a custom class, Invoice. This class consists of a member of Customer class that is a reference type. As a result, deep cloning is used to create a clone of an object of the Invoice class. The code example creates an object of the Invoice class and creates a clone from this object. The code proves that deep cloning is taking place by showing that a change in the customer member of the cloned object does not affect the original object.

Visual Basic

Imports System Public Class Invoice

Implements ICloneable Private _invoiceNo As String Private _amount As Single Private _customer As Customer

Public Sub New(ByVal invoiceNo As String, ByVal amount As Single) Me._invoiceNo = invoiceNo Me._amount = amount End Sub

Public Property Customer() As Customer Get

Return Me._customer End Get

Set(ByVal value As Customer)

Me._customer = value End Set End Property

Public Overrides Function ToString() As String

Return String.Format("No: {0}, Amount: {1:C}, Customer: {2}", _ Me._invoiceNo, Me._amount, Me._customer.Name) End Function

Public Function Clone() As Object _ Implements System.ICloneable.Clone Dim inv As New Invoice(Me._invoiceNo, Me._amount) Dim c As New Customer(Me._customer.Name) inv.Customer = c Return inv End Function

Public Shared Sub Main()

Dim inv As New Invoice("0001", 1000) inv.Customer = New Customer("Stephen")

Dim invClone As Invoice = inv.Clone invClone.Customer.Name = "Thomas" Console.WriteLine(inv.ToString()) Console.WriteLine(invClone.ToString())

Console.Write(ControlChars.NewLine & "Press ENTER to exit. Console. ReadLine() End Sub End Class

Public Class Customer

Private _name As String

Public Sub New(ByVal name As String)

Public Property Name() As String Get

Return Me._name End Get

Set(ByVal value As String)

Me._name = value End Set End Property End Class

using System;

public class Invoice : ICloneable {

private string _invoiceNo; private float _amount; private Customer _customer;

public Invoice(string invoiceNo, float amount) {

this._invoiceNo = invoiceNo; this._amount = amount;

public Customer Customer {

return this._customer;

this._customer = value;

public override string ToString() {

return String.Format("No: {0}, Amount: {1:C}, Customer: {2}", this._invoiceNo, this._amount, this._customer.Name);

public object Clone() {

Invoice inv = new Invoice(this._invoiceNo, this._amount); Customer c = new Customer(this._customer.Name); inv.Customer = c;

return inv;

static void Main(string[] args) {

Invoice inv = new Invoice("0001", 1000); inv.Customer = new Customer("Stephen"); Invoice invClone = (Invoice)inv.C1one(); invClone.Customer.Name = "Thomas"; Conso1e.WriteLine(inv.ToString()); Conso1e.WriteLine(invC1one.ToString()) ; Conso1e.Write("\nPress ENTER to exit..."); Conso1e.ReadLine();

public class Customer {

private string _name;

public Customer(string name) {

public string Name {

return this._name;

Was this article helpful?

0 0

Post a comment