Comment copier un objet sur un autre mais sans que les modifs du 2ème ne
soit aussi répercuté dans le premier ??
Voiçi un exemple qui pose problème :
Public Class Personne
Public Nom As String
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New Personne
Dim p1 As New Personne
p.Nom = "AAAA"
p = p1
p1.Nom = "BBBB"
MsgBox(p.Nom) ' ici, P.nom = "BBB" au leu de "AAA" ????????????????
End Sub
Comment copier un objet sur un autre mais sans que les modifs du 2ème ne soit aussi répercuté dans le premier ??
Utiliser .Clone()
-- Delf
Millox Frédéric
Bonjour,
Dans ton exemple, c'est pas plutôt p1 = p ???
Il faut utiliser l'interface ICloneable :
Public Class Personne Implements ICloneable
Public Nom As String
Public Sub New() End Sub
Public Sub New(ByVal _Nom As String) Nom = _Nom End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone If Nom Is Nothing Then Return New Personne(Nothing) Else Return New Personne(Nom.Clone) End If End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p As New Personne Dim p1 As Personne
p.Nom = "AAAA" p1 = p.Clone p1.Nom = "BBBB"
MsgBox(p.Nom) ' Te retourne bien AAAA End Sub
A+
-- fmillox
"Olivier" a écrit :
Bonjour,
Comment copier un objet sur un autre mais sans que les modifs du 2ème ne soit aussi répercuté dans le premier ??
Voiçi un exemple qui pose problème : Public Class Personne Public Nom As String End Class
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p As New Personne Dim p1 As New Personne p.Nom = "AAAA" p = p1 p1.Nom = "BBBB" MsgBox(p.Nom) ' ici, P.nom = "BBB" au leu de "AAA" ???????????????? End Sub
End Class
Merci Olivier
Bonjour,
Dans ton exemple, c'est pas plutôt p1 = p ???
Il faut utiliser l'interface ICloneable :
Public Class Personne
Implements ICloneable
Public Nom As String
Public Sub New()
End Sub
Public Sub New(ByVal _Nom As String)
Nom = _Nom
End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone
If Nom Is Nothing Then
Return New Personne(Nothing)
Else
Return New Personne(Nom.Clone)
End If
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim p As New Personne
Dim p1 As Personne
p.Nom = "AAAA"
p1 = p.Clone
p1.Nom = "BBBB"
MsgBox(p.Nom) ' Te retourne bien AAAA
End Sub
A+
--
fmillox
"Olivier" a écrit :
Bonjour,
Comment copier un objet sur un autre mais sans que les modifs du 2ème ne
soit aussi répercuté dans le premier ??
Voiçi un exemple qui pose problème :
Public Class Personne
Public Nom As String
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim p As New Personne
Dim p1 As New Personne
p.Nom = "AAAA"
p = p1
p1.Nom = "BBBB"
MsgBox(p.Nom) ' ici, P.nom = "BBB" au leu de "AAA" ????????????????
End Sub
Public Sub New(ByVal _Nom As String) Nom = _Nom End Sub
Public Function Clone() As Object Implements System.ICloneable.Clone If Nom Is Nothing Then Return New Personne(Nothing) Else Return New Personne(Nom.Clone) End If End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p As New Personne Dim p1 As Personne
p.Nom = "AAAA" p1 = p.Clone p1.Nom = "BBBB"
MsgBox(p.Nom) ' Te retourne bien AAAA End Sub
A+
-- fmillox
"Olivier" a écrit :
Bonjour,
Comment copier un objet sur un autre mais sans que les modifs du 2ème ne soit aussi répercuté dans le premier ??
Voiçi un exemple qui pose problème : Public Class Personne Public Nom As String End Class
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim p As New Personne Dim p1 As New Personne p.Nom = "AAAA" p = p1 p1.Nom = "BBBB" MsgBox(p.Nom) ' ici, P.nom = "BBB" au leu de "AAA" ???????????????? End Sub