vb.net can't create different class objects

vb.net can't create different class objects

snappyjazz
Collaborator Collaborator
713 Views
4 Replies
Message 1 of 5

vb.net can't create different class objects

snappyjazz
Collaborator
Collaborator

Hello,

I created a class of objects (Gussets), of which I want to put in 1 of 2 lists. However when I fill the lists with objects (gussets) of that class, any previously created objects are overwritten by the most recently created object. So by then end of the code, I have 2 lists containing multiple objects of the same object (2 lists containing "Gusset:4"). I hope that last part makes sense. Do you know what I'm getting wrong?

Sub Main()
        Dim ExistingIntFBGussList As New List(Of Gusset)
        Dim ExistingIntLRGussList As New List(Of Gusset)

            For Each OccItem As Inv.ComponentOccurrence In AppDoc.AssyCompDef.Occurrences

            Dim GussetType As Gusset = Nothing

            Select Case True
                Case OccItem.Name.Contains("GUSSET,INT,F&B")
                    GussetType = New Gusset(OccItem)
                    ExistingIntFBGussList.Add(GussetType)

                Case OccItem.Name.Contains("GUSSET,INT,L&R")
                    GussetType = New Gusset(OccItem)
                    ExistingIntLRGussList.Add(GussetType)
            End Select
End Sub

    Public Class Gusset


        Protected Shared _CompOcc As Inv.ComponentOccurrence
        Protected Shared GussTrans As Inv.Vector
        Protected Shared DocDef As Inv.PartComponentDefinition
        Protected Shared _Name As String

        Protected Shared _Location As Inv.Point
        Protected Shared _Quadrant As Quad
        Protected Shared _Direction As DirectionEnum
        Protected Shared _Centered As Boolean
        Protected Shared _PlaneXY As Inv.WorkPlane
        Protected Shared _PlaneYZ As Inv.WorkPlane
        Protected Shared _PlaneXZ As Inv.WorkPlane

        Protected Shared _ProxyXY As Inv.WorkPlaneProxy
        Protected Shared _ProxyYZ As Inv.WorkPlaneProxy
        Protected Shared _ProxyXZ As Inv.WorkPlaneProxy
        Protected Shared _i As Integer = 1




        Public Sub New(ByRef ComponentOccurrence As Inv.ComponentOccurrence)
            Console.WriteLine("Class: Gusset | " & _i)
            _i += 1

            _CompOcc = ComponentOccurrence
            GussTrans = _CompOcc.Transformation.Translation
            DocDef = _CompOcc.Definition
            _Name = _CompOcc.Name
            _Location = AppDoc.InvTransGeo.CreatePoint(Convert.Cent2In(GussTrans.X), Convert.Cent2In(GussTrans.Y), Convert.Cent2In(GussTrans.Z))

            'Quardrant
            Select Case True
                Case _Location.X >= 0 AndAlso _Location.Z > 0
                    _Quadrant = Quad.FL
                Case _Location.X >= 0 AndAlso _Location.Z < 0
                    _Quadrant = Quad.RR
                Case _Location.X < 0 AndAlso _Location.Z > 0
                    _Quadrant = Quad.FR
                Case _Location.X < 0 AndAlso _Location.Z < 0
                    _Quadrant = Quad.RL
            End Select

            'Direction
            Select Case True
                Case GussTrans.X > 0 AndAlso GussTrans.Z > 0
                    _Direction = DirectionEnum.BACK
                Case GussTrans.X > 0 AndAlso GussTrans.Z < 0
                    _Direction = DirectionEnum.FRONT
                Case GussTrans.X < 0 AndAlso GussTrans.Z > 0
                    _Direction = DirectionEnum.BACK
                Case GussTrans.X < 0 AndAlso GussTrans.Z < 0
                    _Direction = DirectionEnum.FRONT
            End Select

            'Planes
            _PlaneXY = DocDef.WorkPlanes.Item("XY Plane")
            _PlaneYZ = DocDef.WorkPlanes.Item("YZ Plane")
            _PlaneXZ = DocDef.WorkPlanes.Item("XZ Plane")

            'Proxy
            _CompOcc.CreateGeometryProxy(_PlaneXY, _ProxyXY)
            _CompOcc.CreateGeometryProxy(_PlaneYZ, _ProxyYZ)
            _CompOcc.CreateGeometryProxy(_PlaneXZ, _ProxyXZ)
        End Sub

#Region "Property public: CompOcc"
        Public Property CompOcc As Inv.ComponentOccurrence
            Set(ByVal CompontOccurence As Inv.ComponentOccurrence)

                _CompOcc = CompontOccurence
                GussTrans = _CompOcc.Transformation.Translation
                DocDef = _CompOcc.Definition
                _Name = _CompOcc.Name
                _Location = AppDoc.InvTransGeo.CreatePoint(Convert.Cent2In(GussTrans.X), Convert.Cent2In(GussTrans.Y), Convert.Cent2In(GussTrans.Z))

                'Quardrant
                Select Case True
                    Case _Location.X >= 0 AndAlso _Location.Z > 0
                        _Quadrant = Quad.FL
                    Case _Location.X >= 0 AndAlso _Location.Z < 0
                        _Quadrant = Quad.RR
                    Case _Location.X < 0 AndAlso _Location.Z > 0
                        _Quadrant = Quad.FR
                    Case _Location.X < 0 AndAlso _Location.Z < 0
                        _Quadrant = Quad.RL
                End Select

                'Direction
                Select Case True
                    Case GussTrans.X > 0 AndAlso GussTrans.Z > 0
                        _Direction = DirectionEnum.BACK
                    Case GussTrans.X > 0 AndAlso GussTrans.Z < 0
                        _Direction = DirectionEnum.FRONT
                    Case GussTrans.X < 0 AndAlso GussTrans.Z > 0
                        _Direction = DirectionEnum.BACK
                    Case GussTrans.X < 0 AndAlso GussTrans.Z < 0
                        _Direction = DirectionEnum.FRONT
                End Select

                'Planes
                _PlaneXY = DocDef.WorkPlanes.Item("XY Plane")
                _PlaneYZ = DocDef.WorkPlanes.Item("YZ Plane")
                _PlaneXZ = DocDef.WorkPlanes.Item("XZ Plane")

                'Proxy
                _CompOcc.CreateGeometryProxy(_PlaneXY, _ProxyXY)
                _CompOcc.CreateGeometryProxy(_PlaneYZ, _ProxyYZ)
                _CompOcc.CreateGeometryProxy(_PlaneXZ, _ProxyXZ)
            End Set
            Get
                Return _CompOcc
            End Get
        End Property
#End Region

#Region "Enum: Quad"
        Public Enum Quad
            FR
            RR
            FL
            RL
        End Enum
#End Region

#Region "Property readonly: Name"
        Public ReadOnly Property Name As String
            Get
                Return _Name
            End Get
        End Property
#End Region

#Region "Property readonly: Location"
        Public Property Location As Inv.Point
            Set(Point As Inv.Point)
                _Location = AppDoc.InvTransGeo.CreatePoint(Point.X, Point.Y, Point.Z)
            End Set
            Get
                Return _Location
            End Get
        End Property
#End Region

#Region "Property readonly: Quandrant"
        Public ReadOnly Property Quadrant As String
            Get
                Return _Quadrant
            End Get
        End Property
#End Region

#Region "Enum: Direction"
        Public Enum DirectionEnum
            FRONT
            BACK
            LEFT
            RIGHT
        End Enum
#End Region

#Region "Property readonly: Direction"
        Public ReadOnly Property Direction As String
            Get
                Return _Direction
            End Get
        End Property
#End Region

#Region "Property: Centered"
        Public Property Centered As Boolean
            Set(value As Boolean)
                If _Location.X = 0 OrElse _Location.Z = 0 Then
                    _Centered = True
                Else
                    _Centered = False
                End If
            End Set
            Get
                Return _Centered
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneXY"
        Public ReadOnly Property PlaneXY As Inv.WorkPlane
            Get
                Return _PlaneXY
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneYZ"
        Public ReadOnly Property PlaneYZ As Inv.WorkPlane
            Get
                Return _PlaneYZ
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneXZ"
        Public ReadOnly Property PlaneXZ As Inv.WorkPlane
            Get
                Return _PlaneXZ
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyXY"
        Public ReadOnly Property ProxyXY As Inv.WorkPlaneProxy
            Get
                Return _ProxyXY
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyYZ"
        Public ReadOnly Property ProxyYZ As Inv.WorkPlaneProxy
            Get
                Return _ProxyYZ
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyXZ"
        Public ReadOnly Property ProxyXZ As Inv.WorkPlaneProxy
            Get
                Return _ProxyXZ
            End Get
        End Property
#End Region

    End Class

 

 

0 Likes
Accepted solutions (1)
714 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

You don't have a Next to end your For Each Loop in Sub Main. I'm not sure how it's running without that, it should just give you an error immediately. Other than that I can't see what could be causing issues with the lists. You can try to put a ... in your sub new to see what occurrences it's getting each time.

Console.WriteLine(_Name)

 

0 Likes
Message 3 of 5

snappyjazz
Collaborator
Collaborator

@Anonymous

When copying and pasting the code, I forgot to paste that part. I will recopy and paste with that included. As far as Console.writeline; it's telling me a different component occurrence(gusset) for each call to the class. I'm not sure why it receives a different input(component occurrence) every time, but all objects(gussets) created by that class change to the most recent one. The Black background photo shows that console report. The red boxes show the sub main and class looking at the same component occurrence. The yellow boxes on the bottom are from the sub main confirming how many occurrences are in each list.

 

 

Sub Main()
        Dim ExistingIntFBGussList As New List(Of Gusset)
        Dim ExistingIntLRGussList As New List(Of Gusset)

            For Each OccItem As Inv.ComponentOccurrence In AppDoc.AssyCompDef.Occurrences

            Dim GussetType As Gusset = Nothing

            Select Case True
                Case OccItem.Name.Contains("GUSSET,INT,F&B")
                    GussetType = New Gusset(OccItem)
                    ExistingIntFBGussList.Add(GussetType)
                    Console.WriteLine("FB Item: " & OccItem.Name)
                Case OccItem.Name.Contains("GUSSET,INT,L&R")
                    GussetType = New Gusset(OccItem)
                    ExistingIntLRGussList.Add(GussetType)
                    Console.WriteLine("LR Item: " & OccItem.Name)
            End Select
        Next
        Console.WriteLine("Existing F/B gussets: " & ExistingIntFBGussList.Count & vbNewLine & "Existing L/R gussets: " & ExistingIntLRGussList.Count & vbNewLine & vbNewLine)
End Sub

    Public Class Gusset


        Protected Shared _CompOcc As Inv.ComponentOccurrence
        Protected Shared GussTrans As Inv.Vector
        Protected Shared DocDef As Inv.PartComponentDefinition
        Protected Shared _Name As String

        Protected Shared _Location As Inv.Point
        Protected Shared _Quadrant As Quad
        Protected Shared _Direction As DirectionEnum
        Protected Shared _Centered As Boolean
        Protected Shared _PlaneXY As Inv.WorkPlane
        Protected Shared _PlaneYZ As Inv.WorkPlane
        Protected Shared _PlaneXZ As Inv.WorkPlane

        Protected Shared _ProxyXY As Inv.WorkPlaneProxy
        Protected Shared _ProxyYZ As Inv.WorkPlaneProxy
        Protected Shared _ProxyXZ As Inv.WorkPlaneProxy
        Protected Shared _i As Integer = 1




        Public Sub New(ByRef ComponentOccurrence As Inv.ComponentOccurrence)

            _CompOcc = ComponentOccurrence
            GussTrans = _CompOcc.Transformation.Translation
            DocDef = _CompOcc.Definition
            _Name = _CompOcc.Name
            _Location = AppDoc.InvTransGeo.CreatePoint(Convert.Cent2In(GussTrans.X), Convert.Cent2In(GussTrans.Y), Convert.Cent2In(GussTrans.Z))

            'Quardrant
            Select Case True
                Case _Location.X >= 0 AndAlso _Location.Z > 0
                    _Quadrant = Quad.FL
                Case _Location.X >= 0 AndAlso _Location.Z < 0
                    _Quadrant = Quad.RR
                Case _Location.X < 0 AndAlso _Location.Z > 0
                    _Quadrant = Quad.FR
                Case _Location.X < 0 AndAlso _Location.Z < 0
                    _Quadrant = Quad.RL
            End Select

            'Direction
            Select Case True
                Case GussTrans.X > 0 AndAlso GussTrans.Z > 0
                    _Direction = DirectionEnum.BACK
                Case GussTrans.X > 0 AndAlso GussTrans.Z < 0
                    _Direction = DirectionEnum.FRONT
                Case GussTrans.X < 0 AndAlso GussTrans.Z > 0
                    _Direction = DirectionEnum.BACK
                Case GussTrans.X < 0 AndAlso GussTrans.Z < 0
                    _Direction = DirectionEnum.FRONT
            End Select

            'Planes
            _PlaneXY = DocDef.WorkPlanes.Item("XY Plane")
            _PlaneYZ = DocDef.WorkPlanes.Item("YZ Plane")
            _PlaneXZ = DocDef.WorkPlanes.Item("XZ Plane")

            'Proxy
            _CompOcc.CreateGeometryProxy(_PlaneXY, _ProxyXY)
            _CompOcc.CreateGeometryProxy(_PlaneYZ, _ProxyYZ)
            _CompOcc.CreateGeometryProxy(_PlaneXZ, _ProxyXZ)

            Console.WriteLine("Class: Gusset | " & _i & "  |  " & _CompOcc.Name)
            _i += 1
        End Sub

#Region "Property public: CompOcc"
        Public Property CompOcc As Inv.ComponentOccurrence
            Set(ByVal CompontOccurence As Inv.ComponentOccurrence)

                _CompOcc = CompontOccurence
                GussTrans = _CompOcc.Transformation.Translation
                DocDef = _CompOcc.Definition
                _Name = _CompOcc.Name
                _Location = AppDoc.InvTransGeo.CreatePoint(Convert.Cent2In(GussTrans.X), Convert.Cent2In(GussTrans.Y), Convert.Cent2In(GussTrans.Z))

                'Quardrant
                Select Case True
                    Case _Location.X >= 0 AndAlso _Location.Z > 0
                        _Quadrant = Quad.FL
                    Case _Location.X >= 0 AndAlso _Location.Z < 0
                        _Quadrant = Quad.RR
                    Case _Location.X < 0 AndAlso _Location.Z > 0
                        _Quadrant = Quad.FR
                    Case _Location.X < 0 AndAlso _Location.Z < 0
                        _Quadrant = Quad.RL
                End Select

                'Direction
                Select Case True
                    Case GussTrans.X > 0 AndAlso GussTrans.Z > 0
                        _Direction = DirectionEnum.BACK
                    Case GussTrans.X > 0 AndAlso GussTrans.Z < 0
                        _Direction = DirectionEnum.FRONT
                    Case GussTrans.X < 0 AndAlso GussTrans.Z > 0
                        _Direction = DirectionEnum.BACK
                    Case GussTrans.X < 0 AndAlso GussTrans.Z < 0
                        _Direction = DirectionEnum.FRONT
                End Select

                'Planes
                _PlaneXY = DocDef.WorkPlanes.Item("XY Plane")
                _PlaneYZ = DocDef.WorkPlanes.Item("YZ Plane")
                _PlaneXZ = DocDef.WorkPlanes.Item("XZ Plane")

                'Proxy
                _CompOcc.CreateGeometryProxy(_PlaneXY, _ProxyXY)
                _CompOcc.CreateGeometryProxy(_PlaneYZ, _ProxyYZ)
                _CompOcc.CreateGeometryProxy(_PlaneXZ, _ProxyXZ)
            End Set
            Get
                Return _CompOcc
            End Get
        End Property
#End Region

#Region "Enum: Quad"
        Public Enum Quad
            FR
            RR
            FL
            RL
        End Enum
#End Region

#Region "Property readonly: Name"
        Public ReadOnly Property Name As String
            Get
                Return _Name
            End Get
        End Property
#End Region

#Region "Property readonly: Location"
        Public Property Location As Inv.Point
            Set(Point As Inv.Point)
                _Location = AppDoc.InvTransGeo.CreatePoint(Point.X, Point.Y, Point.Z)
            End Set
            Get
                Return _Location
            End Get
        End Property
#End Region

#Region "Property readonly: Quandrant"
        Public ReadOnly Property Quadrant As String
            Get
                Return _Quadrant
            End Get
        End Property
#End Region

#Region "Enum: Direction"
        Public Enum DirectionEnum
            FRONT
            BACK
            LEFT
            RIGHT
        End Enum
#End Region

#Region "Property readonly: Direction"
        Public ReadOnly Property Direction As String
            Get
                Return _Direction
            End Get
        End Property
#End Region

#Region "Property: Centered"
        Public Property Centered As Boolean
            Set(value As Boolean)
                If _Location.X = 0 OrElse _Location.Z = 0 Then
                    _Centered = True
                Else
                    _Centered = False
                End If
            End Set
            Get
                Return _Centered
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneXY"
        Public ReadOnly Property PlaneXY As Inv.WorkPlane
            Get
                Return _PlaneXY
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneYZ"
        Public ReadOnly Property PlaneYZ As Inv.WorkPlane
            Get
                Return _PlaneYZ
            End Get
        End Property
#End Region

#Region "Property readonly: PlaneXZ"
        Public ReadOnly Property PlaneXZ As Inv.WorkPlane
            Get
                Return _PlaneXZ
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyXY"
        Public ReadOnly Property ProxyXY As Inv.WorkPlaneProxy
            Get
                Return _ProxyXY
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyYZ"
        Public ReadOnly Property ProxyYZ As Inv.WorkPlaneProxy
            Get
                Return _ProxyYZ
            End Get
        End Property
#End Region

#Region "Property readonly: ProxyXZ"
        Public ReadOnly Property ProxyXZ As Inv.WorkPlaneProxy
            Get
                Return _ProxyXZ
            End Get
        End Property
#End Region

    End Class

 

 

image.png 

 

The following photos show the 2 members in the fb list as being the same occurrence "Gusset,int,f&b:2"

image.png

 

image.png

 

 

0 Likes
Message 4 of 5

Anonymous
Not applicable
Accepted solution

I think the issue is you are declaring your Class variables as shared, so they are shared across all instances of the class, so the name of all of them will be what ever was set last.

 

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/shared

Message 5 of 5

snappyjazz
Collaborator
Collaborator

@Anonymous 

Of course it had to be that simple. Thank you!

0 Likes