Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15634

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

$
0
0
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

Viewing all articles
Browse latest Browse all 15634

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>