Clonable Object : Clone « Class « VB.Net






Clonable Object

Clonable Object
Imports System

Public Class MainClass

  Shared Sub Main()
        Dim anArray() As String = {"www.java2s.com"}
        Dim a As New CloneableObject(anArray)
        a.DisplayData()
        Dim b As CloneableObject
        b = CType(a.Clone(), CloneableObject)
        Dim newData As String = "Hi"
        b.ChangeData(newData)
        b.DisplayData()
        a.DisplayData()
  End Sub
End Class

Public Class CloneableObject
    Implements ICloneable
    Private m_Data() As String
    Public Sub New(ByVal anArray() As String)
        m_Data = anArray
    End Sub
    Public Function Clone() As Object _
    Implements ICloneable.Clone
        Dim temp() As String
        temp = CType(m_Data.Clone, String()) 'clone the array
        Return New CloneableObject(temp)
    End Function
    Public Sub DisplayData()
        Dim temp As String
        For Each temp In m_Data
            Console.WriteLine(temp)
        Next
    End Sub
    Public Sub ChangeData(ByVal newData As String)
        m_Data(0) = newData
    End Sub
End Class


           
       








Related examples in the same category

1.Member Wise CloneMember Wise Clone