Results 1 to 3 of 3

Thread: [RESOLVED] Making a copy of a class and modifing it changes original class values PS: With ByVal

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    Resolved [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:
    1. Public Class TriangularFace
    2.         Private arr_Vertices(2) As PointVector
    3.         Public Sub New(ByVal new_Vertex0 As PointVector, ByVal new_Vertex1 As PointVector, ByVal new_Vertex2 As PointVector)
    4.             arr_Vertices(0) = new_Vertex0 : arr_Vertices(1) = new_Vertex1 : arr_Vertices(2) = new_Vertex2
    5.         End Sub
    6.         Public Sub New(ByVal new_Vertices() As PointVector)
    7.             If new_Vertices.Length <> 3 Then
    8.                 Throw New Exception("The array does not have the proper size for a triangular face.")
    9.             Else
    10.                 arr_Vertices = new_Vertices
    11.             End If
    12.         End Sub
    13.         Public Property Vertex_0 As PointVector
    14.             Get
    15.                 Return arr_Vertices(0)
    16.             End Get
    17.             Set(ByVal new_Vertex0 As PointVector)
    18.                 arr_Vertices(0) = new_Vertex0
    19.             End Set
    20.         End Property
    21.         Public Property Vertex_1 As PointVector
    22.             Get
    23.                 Return arr_Vertices(1)
    24.             End Get
    25.             Set(ByVal new_Vertex1 As PointVector)
    26.                 arr_Vertices(1) = new_Vertex1
    27.             End Set
    28.         End Property
    29.         Public Property Vertex_2 As PointVector
    30.             Get
    31.                 Return arr_Vertices(2)
    32.             End Get
    33.             Set(ByVal new_Vertex2 As PointVector)
    34.                 arr_Vertices(2) = new_Vertex2
    35.             End Set
    36.         End Property
    37.     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:
    1. Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    2.         Dim GFX As Graphics = e.Graphics
    3.  
    4.         For Each Triangle As Temp.TriangularFace In Cube.Faces
    5.  
    6.             MsgBox("Original = " & Triangle.Vertex_0.Z & " " & Triangle.Vertex_1.Z & " " & Triangle.Vertex_2.Z)
    7.             Dim ZFixTriangle As New Temp.TriangularFace(Triangle.Vertex_0, Triangle.Vertex_1, Triangle.Vertex_2) 'These two lines affect the z values for PointVectors
    8.             ZFixTriangle.Vertex_0.Z += 3 : ZFixTriangle.Vertex_1.Z += 3 : ZFixTriangle.Vertex_2.Z += 3               'In the "Triangle" variable and not only "ZFixTriangle"
    9.             MsgBox("New = " & ZFixTriangle.Vertex_0.Z & " " & ZFixTriangle.Vertex_1.Z & " " & ZFixTriangle.Vertex_2.Z)
    10.  
    11.         Next
    12. End Sub
    Here is the PointVector Class if needed:
    VB.NET Code:
    1. Public Class PointVector
    2.         Private v_X, v_Y, v_Z, v_W As Single
    3.         Public Sub New(ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
    4.             v_X = X : v_Y = Y : v_Z = Z : v_W = 1
    5.         End Sub
    6.         Public Property X As Single
    7.             Get
    8.                 Return v_X
    9.             End Get
    10.             Set(ByVal new_X As Single)
    11.                 v_X = new_X
    12.             End Set
    13.         End Property
    14.         Public Property Y As Single
    15.             Get
    16.                 Return v_Y
    17.             End Get
    18.             Set(ByVal new_Y As Single)
    19.                 v_Y = new_Y
    20.             End Set
    21.         End Property
    22.         Public Property Z As Single
    23.             Get
    24.                 Return v_Z
    25.             End Get
    26.             Set(ByVal new_Z As Single)
    27.                 v_Z = new_Z
    28.             End Set
    29.         End Property
    30.         Public Property W As Single
    31.             Get
    32.                 Return v_W
    33.             End Get
    34.             Set(ByVal new_W As Single)
    35.                 v_W = new_W
    36.             End Set
    37.         End Property
    38.     End Class

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    80

    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
    Last edited by KBConsole; Jun 16th, 2020 at 01:28 PM. Reason: Grammar

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width