0

我正在加载一个包含 7 个对象的列表,但是这些对象被添加的最后一个对象“覆盖”。有 7 个对象被创建(Exec、Mgr、Position...等)第一个“Exec”被正确添加到列表中,但是创建的每个新 OrgShape 都会覆盖之前添加到列表中的所有 OrgShape . 我知道我错过了一些简单的东西......

Public Shared Function GetOrgShapeData() As List(Of OrgShape)
    Dim OgrShapeList As New List(Of OrgShape)
    Dim conn As OleDbConnection = HR_DB.GetConnection
    Dim strSQL As String
    strSQL = "SELECT * FROM VisioShapeDim"
    Dim selectCommand As New OleDbCommand(strSQL, conn)
    Try
        conn.Open()
        Dim reader As OleDbDataReader = selectCommand.ExecuteReader
        Dim orgshape As OrgShape
        Do While reader.Read
            orgshape = New OrgShape

            orgshape.ShapeName = reader("ShapeName")
            orgshape.ShapeWidth = reader("ShapeWidth")
            orgshape.ShapeHeight = reader("ShapeHeight")

            OgrShapeList.Add(orgshape)
            orgshape = Nothing
        Loop
        reader.Close()
    Catch ex As OleDbException
        Throw ex
    Finally
        conn.Close()
    End Try
    Return OgrShapeList
End Function

'**添加了 OrgShape 类

Public Class OrgShape
  Private Shared m_ShapeName As String
  Private Shared m_ShapeWidth As Double
  Private Shared m_ShapeHeight As Double

  Public Sub New()

  End Sub

  Public Shared Property ShapeName() As String
    Get
        Return m_ShapeName
    End Get
    Set(ByVal value As String)
        m_ShapeName = value
    End Set
  End Property

  Public Shared Property ShapeWidth() As Double
    Get
        Return m_ShapeWidth
    End Get
    Set(ByVal value As Double)
        m_ShapeWidth = value
    End Set
  End Property

  Public Shared Property ShapeHeight() As Double
    Get
        Return m_ShapeHeight
    End Get
    Set(ByVal value As Double)
        m_ShapeHeight = value
    End Set
  End Property
End Class
4

3 回答 3

1

尝试Shared从属性和变量中删除关键字,因为您正在寻找实例:

Public Class OrgShape
  Private m_ShapeName As String
  Private m_ShapeWidth As Double
  Private m_ShapeHeight As Double

  Public Property ShapeName() As String
    Get
      Return m_ShapeName
    End Get
    Set(ByVal value As String)
      m_ShapeName = value
    End Set
  End Property

请参阅共享 (Visual Basic)

于 2013-01-18T16:46:05.400 回答
0

我会Dim orgshape As new OrgShape在循环中使用并删除orgshape = New OrgShapeorgshape = Nothing

于 2013-01-18T16:46:05.360 回答
0

您需要删除以下行

orgshape = Nothing

你每次都在擦除你的对象。

请记住,在 .NET 中,对象是reference类型。

根据问题编辑进行编辑。您也不能将共享字段和属性用于实例数据。

于 2013-01-18T16:41:42.013 回答