1

我对 VB.NET 编程很陌生,我有这种情况:

我有一个类 Foo 有一些私有字段(数量可以增加,所以我想写一些灵活的代码)及其相应的公共只读属性。为了更新私有字段的值,我必须在 OPC 服务器中读取它们。当我注册到一个 OPCServer 项目时,我得到一个整数,称为 ServerHandle,来识别它。然后,当我读取 OPC 服务器时,我得到了几个 ServerHandles 及其对应的值,以字典的形式(serverHandles 作为键)。

我想在创建对象时创建一个辅助对象列表(我称它们为 Item),其中只有两个公共字段 ServerHandle 和对私有字段的引用,所以当我得到更新值字典:

Public Class Foo
    Private field1 As Double
    Private field2 As Double
    Private listOfitems As List(Of Item)

    Private Sub UpdateValues(dictionaryOfValues As Dictionary(Of Integer, Double))
        For Each item As Item In listOfitems
            item.Field = dictionaryOfValues(item.ServerHandle)
        Next
    End Sub
End Class


Public Class Item
    Public Field As Object
    Public ServerHandle As Integer
End Class

我知道不可能像这样保存对私有字段的引用......但我想知道是否有某种方法可以做类似于我正在尝试的事情。

如果没有...您对我该怎么做有什么建议吗?(我觉得我不必要地使我的解决方案复杂化)。

非常感谢!

4

1 回答 1

0

你的类需要某种类型的publicly 暴露对象Foo,我建议为 设置一个只读的公共属性listOfItems,如下所示:

Public Class Foo
    Private field1 As Double
    Private field2 As Double
    Private listOfitems As List(Of Item)

    Public Property ListOfItems() As List(Of Integer)
    Get
    Return listOfitems
    End Get
    End Property

    Private Sub UpdateValues(dictionaryOfValues As Dictionary(Of Integer, Double))
        For Each item As Item In listOfitems
            item.Field = dictionaryOfValues(item.ServerHandle)
        Next
    End Sub
End Class
于 2013-08-04T20:58:45.883 回答