以前の 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 = "Trey"
この配列構造に何らかの機能を追加したいので、クラスを作成しました。これはデータを保持し、クラス内で何らかの操作を行います (必要に応じてデータを反転するなど)。次に、クラスを作成し、クラスのリストを生成しました。
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"
これは動作しません。リスト内のすべての項目をループせずに、リスト内の特定の要素にアクセスする方法がわかりません。
何かご意見は?