【问题标题】:VBNet: List of items inside a class, I cant change the value for a single itemVB Net:类内的项目列表,我无法更改单个项目的值
【发布时间】:2020-08-22 14:27:33
【问题描述】:

我的问题: 我有一个类和一个其他类的列表:

Public Class Signal_Type_Read

    Private c_signal_count As Integer = 0  ' counter for read signals
    Private _items As List(Of Signal_Item)
    Private item As New Signal_Item

    Sub add_sig()
        c_signal_count += 1
        items.Add(item)
    End Sub
    Public Property items() As List(Of Signal_Item)
        Get
            Return _items
        End Get
        Set(value As List(Of Signal_Item))
            _items = value
        End Set
    End Property
    Function item_counter() As Integer
        item_counter = c_signal_count
    End Function
    Public Sub New()
        _items = New List(Of Signal_Item)
    End Sub
End Class

Public Class Signal_Item


    Private _original_name As String

    Public Property Original_name() As String
        Get
            Return _original_name
        End Get
        Set(value As String)
            _original_name = value
        End Set
    End Property

    'Many other properties
End Class

我的问题是当我在循环中使用时

Public Shared ReadSignals As New Signal_Type_Read 

//Part of a Loop to read cells values and store in the variable
ReadSignals.add_sig()
Dim c_index As Integer = ReadSignals.item_counter - 1                   
ReadSignals.items.item(c_index).Original_name = c_row.Cells(e_Signame).Value

它总是会更改我的变量的所有项目中的“Original_name”属性。我的错误在哪里?我只想改变那个 oe 项目。

【问题讨论】:

    标签: arrays list class properties


    【解决方案1】:

    我找到了问题的原因...我需要在我的 ADD_sig() 子中创建一个新的项目实例

     Sub add_sig()
        Dim s_item As New Signal_Item
        c_signal_count += 1
        items.Add(s_item)
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-04
      • 2015-12-18
      • 2017-09-27
      • 2020-03-24
      • 2021-11-05
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      相关资源
      最近更新 更多