Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何访问列表中所需的项目?_Vb.net_List_Collections - Fatal编程技术网

Vb.net 如何访问列表中所需的项目?

Vb.net 如何访问列表中所需的项目?,vb.net,list,collections,Vb.net,List,Collections,我正在将以前的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” 因为我想添加这个阵列结构的一些功能,所以我

我正在将以前的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"
这是行不通的。我不知道如何访问列表中的特定元素,而不遍历列表中的所有项目


有什么想法吗?

从类变量声明中删除
Shared
关键字。您将这些定义为类变量,这样类的每个实例都没有自己的副本。这意味着上一次更新将覆盖上一次更新,更改该类的任何对象都将影响所有对象。

从类变量声明中删除
Shared
关键字。您将这些定义为类变量,这样类的每个实例都没有自己的副本。这意味着上一次更新会覆盖上一次更新,更改该类的任何对象都会影响所有对象。

您能显示
DInList
的声明吗?我看不到您对
DInList
的声明。另外,“这不起作用”是什么意思?具体点。根据您编写的内容,并假设您将DInList声明编写为
Dim DInList as New List(Of clsIO)
,它应该可以工作……您的意思是共享您的私有变量吗?这意味着它们属于类,而不是对象的实例。您能显示
DInList
的声明吗?我看不到您的
DInList
声明。另外,“这不起作用”是什么意思?具体点。根据您编写的内容,并假设您将DInList声明编写为
Dim DInList as New List(Of clsIO)
,它应该可以工作……您的意思是共享您的私有变量吗?这意味着它们属于类,而不是对象的实例。