VB.Net/Class/Clone

Материал из VB Эксперт
Версия от 12:42, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Clonable Object

Imports System
Public Class MainClass
  Shared Sub Main()
        Dim anArray() As String = {"www.vbex.ru"}
        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


Member Wise Clone

Imports System
Imports System.Collections

Public Class MainClass
  Shared Sub Main()
        Dim anArray() As String = {"www.vbex.ru"}
        Dim a As New CloneableObject(anArray)
        a.DisplayData()
        Dim b As CloneableObject
        b = a.Clone()
        Dim newData As String = "New Data"
        b.ChangeData(newData)
        b.DisplayData()
        a.DisplayData()
  End Sub
  
End Class
Public Class CloneableObject
    
    Private m_Data() As String
    
    Public Sub New(ByVal anArray() As String)
        m_Data = anArray
    End Sub
    
    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
    
    Public Function Clone() As CloneableObject
        Return CType(Me.MemberwiseClone, CloneableObject)
    End Function
End Class