我正在加载一个包含7个对象的列表,但是对象会被添加的最后一个对象“覆盖”。创建了7个对象(Exec,Mgr,Position ...等)第一个“Exec”被正确添加到列表中,但是创建的每个新OrgShape都会覆盖之前添加到列表中的所有OrgShapes 。我知道我很遗憾......
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
答案 0 :(得分: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
答案 1 :(得分:0)
您需要删除以下行
orgshape = Nothing
你每次都在擦除你的物体。
请记住,.NET对象是reference
类型。
根据问题编辑进行编辑。您也不能将共享字段和属性用于实例数据。
答案 2 :(得分:0)
我会在循环中使用Dim orgshape As new OrgShape
并删除orgshape = New OrgShape
和orgshape = Nothing