[RESOLVED] Making a copy of a class and modifing it changes original class values PS: With ByVal
I have recently been trying to learn 3D and create a small 3DEngine.
I made the following Class:
VB.NET Code:
Public Class TriangularFace
Private arr_Vertices(2) As PointVector
Public Sub New(ByVal new_Vertex0 As PointVector, ByVal new_Vertex1 As PointVector, ByVal new_Vertex2 As PointVector)
arr_Vertices(0) = new_Vertex0 : arr_Vertices(1) = new_Vertex1 : arr_Vertices(2) = new_Vertex2
End Sub
Public Sub New(ByVal new_Vertices() As PointVector)
If new_Vertices.Length <> 3 Then
Throw New Exception("The array does not have the proper size for a triangular face.")
Else
arr_Vertices = new_Vertices
End If
End Sub
Public Property Vertex_0 As PointVector
Get
Return arr_Vertices(0)
End Get
Set(ByVal new_Vertex0 As PointVector)
arr_Vertices(0) = new_Vertex0
End Set
End Property
Public Property Vertex_1 As PointVector
Get
Return arr_Vertices(1)
End Get
Set(ByVal new_Vertex1 As PointVector)
arr_Vertices(1) = new_Vertex1
End Set
End Property
Public Property Vertex_2 As PointVector
Get
Return arr_Vertices(2)
End Get
Set(ByVal new_Vertex2 As PointVector)
arr_Vertices(2) = new_Vertex2
End Set
End Property
End Class
The issue is that assuming I make a new Triangle based on the values of an existing triangle and then change it's values, the original triangle will also see it's values modified:
VB.NET Code:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim GFX As Graphics = e.Graphics
For Each Triangle As Temp.TriangularFace In Cube.Faces
MsgBox("Original = " & Triangle.Vertex_0.Z & " " & Triangle.Vertex_1.Z & " " & Triangle.Vertex_2.Z)
Dim ZFixTriangle As New Temp.TriangularFace(Triangle.Vertex_0, Triangle.Vertex_1, Triangle.Vertex_2) 'These two lines affect the z values for PointVectors
ZFixTriangle.Vertex_0.Z += 3 : ZFixTriangle.Vertex_1.Z += 3 : ZFixTriangle.Vertex_2.Z += 3 'In the "Triangle" variable and not only "ZFixTriangle"
MsgBox("New = " & ZFixTriangle.Vertex_0.Z & " " & ZFixTriangle.Vertex_1.Z & " " & ZFixTriangle.Vertex_2.Z)
Next
End Sub
Here is the PointVector Class if needed:
VB.NET Code:
Public Class PointVector
Private v_X, v_Y, v_Z, v_W As Single
Public Sub New(ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
v_X = X : v_Y = Y : v_Z = Z : v_W = 1
End Sub
Public Property X As Single
Get
Return v_X
End Get
Set(ByVal new_X As Single)
v_X = new_X
End Set
End Property
Public Property Y As Single
Get
Return v_Y
End Get
Set(ByVal new_Y As Single)
v_Y = new_Y
End Set
End Property
Public Property Z As Single
Get
Return v_Z
End Get
Set(ByVal new_Z As Single)
v_Z = new_Z
End Set
End Property
Public Property W As Single
Get
Return v_W
End Get
Set(ByVal new_W As Single)
v_W = new_W
End Set
End Property
End Class
Re: Making a copy of a class and modifing it changes original class values PS: With B
Your Vertex properties are of type PointVector... but PointVector is a class, so when you assign a value of PointVector to a variable/property/etc you actually assign a pointer to the same item - not a copy.
That means in the line you create ZFixTriangle, you are actually linking it to the instance you passed in (so Triangle.Vertex_0 etc).
What you need to do is create a Clone of the instances instead, which you can do by creating a method for that in the PointVector class, eg:
Code:
Public Function Clone() as PointVector
Dim clonedInstance as New PointVector(v_X, v_Y, v_Z)
clonedInstance.W = v_W
Return clonedInstance
End Function
...and then call this either in the line that initialises ZFixTriangle, or within the constructor for TriangularFace.
Re: Making a copy of a class and modifing it changes original class values PS: With B
Thank you sir, simple and accurate answer making it an easy fix for me :)