我將之前的VB6代碼轉換爲.Net(2012),並且正在創建一個包含曾經在數組中的數據的類。如何訪問列表中的所需項目?
Structure defIO
dim Index as integer
dim Name as string
dim State as Boolean
dim Invert as Boolean
end structure
public IO(128) as defIO
現在我可以在陣列中訪問的每個元素:IO(3)請將.Name =「特雷」
由於我要添加這一陣列結構的某些功能,我創建的類。這保存了數據,並會在課堂上對我進行一些操作(如果需要的話反轉數據等)。然後我創建了這個類並生成了一個類的列表。
Public Class clsIO
Private Shared pState As Boolean
Private Shared pInvert As Boolean
Private Shared pIndex As Integer
Private Shared pName As String
Public Sub New()
Try
pState = False
pInvert = False
pIndex = 0
Catch ex As Exception
MsgBox("Exception caught!" & vbCrLf & ex.TargetSite.Name & vbCrLf & ex.Message)
End Try
End Sub
Property Name As String
Get
Name = pName
End Get
Set(value As String)
pName = value
End Set
End Property
Property State As Boolean
Get
State = pState
End Get
Set(value As Boolean)
If pInvert = True Then
pState = Not value
Else
pState = value
End If
End Set
End Property
Property Invert As Boolean
Get
Invert = pInvert
End Get
Set(value As Boolean)
pInvert = value
End Set
End Property
Property Index As Integer
Get
Index = pIndex
End Get
Set(value As Integer)
pIndex = value
End Set
End Property
End Class
DInList.Add(New clsIO() With {.Index = 0, .Name = "T1ShutterInPos", .Invert = False, .State = False})
DInList.Add(New clsIO() With {.Index = 1, .Name = "T2ShutterInPos", .Invert = False, .State = False})
DInList.Add(New clsIO() With {.Index = 2, .Name = "T3ShutterInPos", .Invert = False, .State = False})
DInList.Add(New clsIO() With {.Index = 3, .Name = "RotationPos1", .Invert = False, .State = False})
DInList.Add(New clsIO() With {.Index = 4, .Name = "RotationPos2", .Invert = False, .State = False})
DInList.Add(New clsIO() With {.Index = 5, .Name = "RotationPos3", .Invert = False, .State = False})
現在我想在列表中訪問特定的元素:
DInList(1).Name = "Test"
這是行不通的。我不知道如何訪問列表中的特定元素,而無需循環列表中的所有項目。
有什麼想法?
你可以顯示'DInList'的聲明嗎? – shahkalpesh 2013-03-20 19:15:12
我沒有看到你的'DInList'聲明。 – 2013-03-20 19:15:17
另外,「這不起作用」是什麼意思?請明確點。 – 2013-03-20 19:15:40