4 Replies - 935 Views - Last Post: 16 November 2012 - 02:22 PM Rate Topic: -----

#1 kcimos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 12

generic lists - need values, not references

Posted 16 November 2012 - 11:56 AM

I created a class with about 40 properties in it

In another class I have created of generic list of the above class (lets just call it "A" here, & the list "Alist")

It seemed to me that the best way of populating my list would be to create a temporary variable of type "A" (let's call it "tempA"), do a wide variety of things to set the various properties of tempA, which involves creating a bunch of different objects (layer table records for example), and then adding tempA to Alist as follows:


Alist.add(tempA)


the problem with this is that every item in my list ends up being a reference to tempA,


so Instead of using tempA, I just directly set the current list member by keeping track of my count (which now starts getting into the extra code required that I'm trying to avoid by using lists in the first place). But this only "works" if I set the property to an actual value such as TRUE, or 15, etc, otherwise all list memebers still reference the same object such as a layertablerecord. So in a list of layernames that should be "a", "b",..."z", I end up with "z","z",..."z".


I do not want to create constructors for my class, & end up with :


Alist.add(layouttype, layoutname,layername,ison,isfrozen, etc, etc & 35 more items would go in this series)

which would actually end up looking more like this:

Alist.add(true, strlayoutname, false,"PS", vpnumber,LVP.Color.ColorNameForDisplay, LTR.Color.ColorNameForDisplay,LTR.Color, & on & on & on )



is there a way to force or convert the referenced data to be byval?

should I just use an array instead?

why are lists byref to begin with? what good does that do?

Is This A Good Question/Topic? 0
  • +

Replies To: generic lists - need values, not references

#2 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: generic lists - need values, not references

Posted 16 November 2012 - 12:13 PM

There's a lot of text, and no code, so all is speculation, but let's simplify things:
Class A
Public Property Name As String
End Class

...somewhere else in one method:
Dim myListOfA As New List(Of A)
For n = 1 to 10
   Dim newInstanceOfA As New A With {.Name=n.ToString}
   myListOfA.Add(newInstanceOfA)
Next

'you'll have a list with 10 different objects


Was This Post Helpful? 0
  • +
  • -

#3 kcimos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 12

Re: generic lists - need values, not references

Posted 16 November 2012 - 01:23 PM

well, here is my class, it has 25 properties (the one with 40 is in another project):
Public Class FullLayerData

    Private strLayoutName As String
    Private strSpaceName As String

    Private strLayerName As String
    Private strColorName As String
    Private strLinetypeName As String
    Private strLineweightName As String
    Private strPlotStyleName As String

    Private strVpLayerColorName As String
    Private strVpLinetypeName As String
    Private strVpLineweightName As String
    Private strVpPlotStyleName As String

    Private booIsLayout As Boolean
    Private booIsModelSpace As Boolean
    Private booLayerON As Boolean
    Private booLayerFrozen As Boolean
    Private booLayerVpFrozen As Boolean
    Private booLayerNoPlot As Boolean
    Private booLayerNewFrozen As Boolean

    Private booIsColorVpOverridden As Boolean
    Private booIsLinetypVpOverridden As Boolean
    Private booIsLineweightVpOverridden As Boolean
    Private booIsPlotStyleVpOverridden As Boolean

    Private iVpNumber As Integer

    Private colLayerColor As Autodesk.AutoCAD.Colors.Color
    Private colVpLayerColor As Autodesk.AutoCAD.Colors.Color


    Public Property LayoutName() As String

        Get

            Return strLayoutName

        End Get
        Set(ByVal value As String)

            strLayoutName = value

        End Set
    End Property

    Public Property SpaceName() As String

        Get

            Return strSpaceName

        End Get
        Set(ByVal value As String)

            strSpaceName = value

        End Set
    End Property

    Public Property LayerName() As String

        Get

            Return strLayerName

        End Get
        Set(ByVal value As String)

            strLayerName = value

        End Set
    End Property

    Public Property ColorName() As String

        Get

            Return strColorName

        End Get
        Set(ByVal value As String)

            strColorName = value

        End Set
    End Property

    Public Property LinetypeName() As String

        Get

            Return strLinetypeName

        End Get
        Set(ByVal value As String)

            strLinetypeName = value

        End Set
    End Property

    Public Property LineweightName() As String

        Get

            Return strLineweightName

        End Get
        Set(ByVal value As String)

            strLineweightName = value

        End Set
    End Property

    Public Property PlotStyleName() As String

        Get

            Return strPlotStyleName

        End Get
        Set(ByVal value As String)

            strPlotStyleName = value

        End Set
    End Property

    Public Property VpLayerColorName() As String

        Get

            Return strVpLayerColorName

        End Get
        Set(ByVal value As String)

            strVpLayerColorName = value

        End Set
    End Property

    Public Property VpLinetypeName() As String

        Get

            Return strVpLinetypeName

        End Get
        Set(ByVal value As String)

            strVpLinetypeName = value

        End Set
    End Property

    Public Property VpLineweightName() As String

        Get

            Return strVpLineweightName

        End Get
        Set(ByVal value As String)

            strVpLineweightName = value

        End Set
    End Property

    Public Property VpPlotStyleName() As String

        Get

            Return strVpPlotStyleName

        End Get
        Set(ByVal value As String)

            strVpPlotStyleName = value

        End Set
    End Property





    Public Property IsLayout() As Boolean

        Get

            Return booIsLayout

        End Get
        Set(ByVal value As Boolean)

            booIsLayout = value

        End Set
    End Property
   
    Public Property IsModelSpace() As Boolean

        Get

            Return booIsModelSpace

        End Get
        Set(ByVal value As Boolean)

            booIsModelSpace = value

        End Set
    End Property

    Public Property LayerON() As Boolean

        Get

            Return booLayerON

        End Get
        Set(ByVal value As Boolean)

            booLayerON = value

        End Set
    End Property

    Public Property LayerFrozen() As Boolean

        Get

            Return booLayerFrozen

        End Get
        Set(ByVal value As Boolean)

            booLayerFrozen = value

        End Set
    End Property

    Public Property LayerVpFrozen() As Boolean

        Get

            Return booLayerVpFrozen

        End Get
        Set(ByVal value As Boolean)

            booLayerVpFrozen = value

        End Set
    End Property

    Public Property LayerNoPlot() As Boolean

        Get

            Return booLayerNoPlot

        End Get
        Set(ByVal value As Boolean)

            booLayerNoPlot = value

        End Set
    End Property

    Public Property LayerNewFrozen() As Boolean

        Get

            Return booLayerNewFrozen

        End Get
        Set(ByVal value As Boolean)

            booLayerNewFrozen = value

        End Set
    End Property

    Public Property IsColorVpOverridden() As Boolean

        Get

            Return booIsColorVpOverridden

        End Get
        Set(ByVal value As Boolean)

            booIsColorVpOverridden = value

        End Set
    End Property

    Public Property IsLinetypVpOverridden() As Boolean

        Get

            Return booIsLinetypVpOverridden

        End Get
        Set(ByVal value As Boolean)

            booIsLinetypVpOverridden = value

        End Set
    End Property

    Public Property IsLineweightVpOverridden() As Boolean

        Get

            Return booIsLineweightVpOverridden

        End Get
        Set(ByVal value As Boolean)

            booIsLineweightVpOverridden = value

        End Set
    End Property


    Public Property IsPlotStyleVpOverridden() As Boolean

        Get

            Return booIsPlotStyleVpOverridden

        End Get
        Set(ByVal value As Boolean)

            booIsPlotStyleVpOverridden = value

        End Set
    End Property


    Public Property VpNumber() As Integer

        Get

            Return iVpNumber

        End Get
        Set(ByVal value As Integer)

            iVpNumber = value

        End Set
    End Property

    Public Property LayerColor() As Autodesk.AutoCAD.Colors.Color

        Get

            Return colLayerColor

        End Get
        Set(ByVal value As Autodesk.AutoCAD.Colors.Color)

            colLayerColor = value

        End Set
    End Property


    Public Property VpLayerColor() As Autodesk.AutoCAD.Colors.Color

        Get

            Return colVpLayerColor

        End Get
        Set(ByVal value As Autodesk.AutoCAD.Colors.Color)

            colVpLayerColor = value

        End Set
    End Property

End Class


I could post my procedures that use this class, but there is a lot of code. but I've got this sort of thing going on in it:
     Shared liFullLayerDataList As New List(Of FullLayerData)
    Shared fldNewFLD As FullLayerData
  
    Public Property FullLayerDataList() As List(Of FullLayerData)

        Get

            Return liFullLayerDataList

        End Get

        Set(ByVal value As List(Of FullLayerData))

            liFullLayerDataList = value

        End Set

    End Property

    Private Property NewFLD() As FullLayerData

        Get

            Return fldNewFLD

        End Get

        Set(ByVal value As FullLayerData)

            fldNewFLD = value

        End Set

    End Property

[\code]
& then
[code]


               NewFLD = New FullLayerData

                    FullLayerDataList.Add(NewFLD)

                    FullLayerDataList.Item(FLDcount).IsLayout = False
                    FullLayerDataList.Item(FLDcount).LayoutName = strLayoutName
                    FullLayerDataList.Item(FLDcount).IsModelSpace = False
                    FullLayerDataList.Item(FLDcount).SpaceName = "Model"

Was This Post Helpful? 0
  • +
  • -

#4 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: generic lists - need values, not references

Posted 16 November 2012 - 02:09 PM

Just post the procedure where you add items to the list, and delete redundant code out of it. In your code above, you add it one time, so I couldn't tell why is always the same, if it's just one item added.

If you do it like this:
Class A
  Public Name As String
End Class

Dim instanceA As New A With {.Name="a"}
Dim listOfInstancesA As New List(Of A)

For n = 1 to 10
   instanceA.Name = n.ToString
   listOfInstancesA.Add(instanceA)
Next




then all items in the list would be the same object, with Name property "10". If you'd declare InstanceA inside For loop, you'd have ten different objects in the list.
Was This Post Helpful? 0
  • +
  • -

#5 kcimos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-November 12

Re: generic lists - need values, not references

Posted 16 November 2012 - 02:22 PM

one thing I'm trying to avoid is this:

Dim instanceA As New A With {.Name="a",.this = that,.that = that,.theotherthing = somethingelse,...}

becuase I have 25 properties. aside from the tedium of that, the values of these properties are often extracted from other objects. So instead of a using "a", it might instead be layerobjectrecord.layername (which might be "a")

I have checked my procedures while debugging & I can see that for each iteration the properties are being extracted properly and the new list item reflects these values. The problem is thatin the next iteration the previous list members' property values all reflect the current iteration.

but if this helps you help me:
 Private Sub AddFullLayerDataForEachAutocadVP()

        Dim LayLayout As Layout
        Dim strLayoutName As String
        Dim dictLayouts As DBDictionary
        Dim vpVP As Viewport
        'Dim iVpNum As Integer

        Dim iCurrentCvports As Integer = CInt(Application.GetSystemVariable("CVPORT"))
        Dim iCurrentTilemode As Integer = CInt(Application.GetSystemVariable("TILEMODE"))

        Dim MeaninglessId As ObjectId

        Dim FLDcount As Integer

        FLDcount = 0

        FullLayerDataList.Clear()
        FullLayerDataList.TrimExcess()

        Using Tx As Transaction = ACF.Db.TransactionManager.StartTransaction

            LT = CType(Tx.GetObject(ACF.Db.LayerTableId, OpenMode.ForRead), LayerTable)

            dictLayouts = CType(Tx.GetObject(ACF.Db.LayoutDictionaryId, OpenMode.ForRead), DBDictionary)


            For Each DE1 As DBDictionaryEntry In dictLayouts

                If DE1.Key.ToString() = "Model" Then

                    strLayoutName = "Model"

                    LayoutManager.Current.CurrentLayout = strLayoutName
                    Application.SetSystemVariable("TILEMODE", 1)

                    NewFLD = New FullLayerData

                    FullLayerDataList.Add(NewFLD)

                    FullLayerDataList.Item(FLDcount).IsLayout = False
                    FullLayerDataList.Item(FLDcount).LayoutName = strLayoutName
                    FullLayerDataList.Item(FLDcount).IsModelSpace = False
                    FullLayerDataList.Item(FLDcount).SpaceName = "Model"

                    MeaninglessId = DE1.Value

                    ReadLayerDataFromAutocad(DE1, False, MeaninglessId, FLDcount)

                    NewFLD = Nothing
                    FLDcount = FLDcount + 1

                Else
                End If



            Next DE1

            For Each DE As DBDictionaryEntry In dictLayouts

                If Not DE.Key.ToString() = "Model" Then

                    LayLayout = CType(Tx.GetObject(CType(DE.Value, ObjectId), OpenMode.ForRead), Layout)

                    strLayoutName = LayLayout.LayoutName

                    LayoutManager.Current.CurrentLayout = strLayoutName

                    For Each id As ObjectId In LayLayout.GetViewports

                        NewFLD = New FullLayerData

                        FullLayerDataList.Add(NewFLD)



                        vpVP = CType(Tx.GetObject(id, OpenMode.ForRead), Viewport)
                        NewFLD.VpNumber = vpVP.Number

                        If NewFLD.VpNumber = 1 Then         'first VP should be PS, strVpNum is nothing until first VP is iterated

                            ACF.Ed.SwitchToPaperSpace()
                            Application.SetSystemVariable("cvport", NewFLD.VpNumber)

                            FullLayerDataList.Item(FLDcount).IsLayout = True
                            FullLayerDataList.Item(FLDcount).LayoutName = strLayoutName
                            FullLayerDataList.Item(FLDcount).IsModelSpace = False
                            FullLayerDataList.Item(FLDcount).SpaceName = "PS"
                            FullLayerDataList.Item(FLDcount).VpNumber = NewFLD.VpNumber

                            ReadLayerDataFromAutocad(DE, True, id, FLDcount)

                            NewFLD = Nothing

                            ACF.Ed.SwitchToPaperSpace()

                        Else

                            ACF.Ed.SwitchToModelSpace()
                            Application.SetSystemVariable("cvport", NewFLD.VpNumber)
                            vpVP.UpdateDisplay()

                            FullLayerDataList.Item(FLDcount).IsLayout = True
                            FullLayerDataList.Item(FLDcount).LayoutName = strLayoutName
                            FullLayerDataList.Item(FLDcount).IsModelSpace = True
                            FullLayerDataList.Item(FLDcount).SpaceName = "MS"
                            FullLayerDataList.Item(FLDcount).VpNumber = NewFLD.VpNumber

                            ReadLayerDataFromAutocad(DE, True, id, FLDcount)

                            NewFLD = Nothing

                            ACF.Ed.SwitchToPaperSpace()

                        End If

                        FLDcount = FLDcount + 1

                    Next id
                End If
            Next DE

        End Using

    End Sub

    Public Sub ReadLayerDataFromAutocad(ByVal subDE As DBDictionaryEntry, ByRef SubIsLayout As Boolean, ByVal subID As ObjectId, ByRef subFLDcount As Integer)         'SubIsLayout needs to set to false for the model tab, & true for all layout tabs

        Dim LTR As LayerTableRecord

        Dim idLineType As ObjectId
        Dim LTTR As LinetypeTableRecord

        Dim LVP As LayerViewportProperties
        Dim vpVP1 As Viewport

        For Each LTRid As ObjectId In LT

            ' Dim supertempFLD = New FullLayerData



            Using Tx1 As Transaction = ACF.Db.TransactionManager.StartTransaction

                LTR = CType(Tx1.GetObject(LTRid, OpenMode.ForRead), LayerTableRecord)

                FullLayerDataList.Item(subFLDcount).LayerName = LTR.Name

                FullLayerDataList.Item(subFLDcount).ColorName = LTR.Color.ColorNameForDisplay
                FullLayerDataList.Item(subFLDcount).LayerColor = LTR.Color

                If SubIsLayout = "true" Then

                    LVP = LTR.GetViewportOverrides(subID)

                    vpVP1 = CType(Tx1.GetObject(subID, OpenMode.ForRead), Viewport)



                    If LVP.IsColorOverridden = True Then

                        FullLayerDataList.Item(subFLDcount).IsColorVpOverridden = True
                        FullLayerDataList.Item(subFLDcount).VpLayerColorName = LVP.Color.ColorNameForDisplay
                        FullLayerDataList.Item(subFLDcount).VpLayerColor = LVP.Color

                        'rRange.Font.Color = Col2Str(LVP.Color)          '/<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

                    Else
                    End If

                    If LVP.IsPlotStyleOverridden = True Then

                        FullLayerDataList.Item(subFLDcount).IsPlotStyleVpOverridden = True
                        FullLayerDataList.Item(subFLDcount).VpPlotStyleName = LVP.PlotStyleName

                    Else
                    End If

                    If LVP.IsLinetypeOverridden = True Then

                        idLineType = LVP.LinetypeObjectId
                        LTTR = CType(Tx1.GetObject(idLineType, OpenMode.ForRead), LinetypeTableRecord)

                        FullLayerDataList.Item(subFLDcount).IsLinetypVpOverridden = True
                        FullLayerDataList.Item(subFLDcount).VpLinetypeName = LTTR.Name

                    Else
                    End If

                    If vpVP1.IsLayerFrozenInViewport(LTRid) = True Then  'freeze = yes

                        FullLayerDataList.Item(subFLDcount).LayerVpFrozen = True

                    Else
                    End If

                End If

                FullLayerDataList.Item(subFLDcount).PlotStyleName = LTR.PlotStyleName

                idLineType = LTR.LinetypeObjectId
                LTTR = CType(Tx1.GetObject(idLineType, OpenMode.ForRead), LinetypeTableRecord)

                FullLayerDataList.Item(subFLDcount).LinetypeName = LTTR.Name

                If LTR.IsOff = True Then

                Else

                    FullLayerDataList.Item(subFLDcount).LayerON = True

                End If

                If LTR.IsFrozen = True Then  'freeze = yes

                    FullLayerDataList.Item(subFLDcount).LayerFrozen = True

                Else     'freeze = no

                End If

                If LTR.IsPlottable = True Then 'noplot = no

                Else

                    FullLayerDataList.Item(subFLDcount).LayerNoPlot = True

                End If

            End Using






        Next

    End Sub

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1