如何创建常量列表

How to create a List of constants

我在另一个 class 中有一个 class,它具有 public 访问权限,名为 SampleRating。 有一个名为 SampleRatingsSampleRating 类型的对象列表。 存储在列表项中的数据在初始设置后不应更改,只能查询。列表本身也不应更改。

使列表及其中的值具有只读的正确方法是什么属性?我尝试了 ConstStatic,但编译器对两者都提出了抱怨。

我的 class 和列表:

Public Class Foo
    Public Class SampleRating
        Enum SampleRateSetting As Byte
            SR0 = 1
            SR1 = 2
            SR2 = 4
            SR3 = 8
        End Enum

        Public Setting As SampleRateSetting
        Public Noise As UInt16
        Public Bandwidth As UInt16
        Public SampleRate As UInt32
        Public BinWidth As Double

        Public Shared Function FindSetting(Setting As SampleRateSetting) As Predicate(Of SampleRating)
            Return Function(Rating As SampleRating) Rating.Setting = Setting
        End Function
    End Class

    Public Shared SampleRatings As New List(Of SampleRating) From {
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR0, .SampleRate = 100189, .BinWidth = 196, .Bandwidth = 26000, .Noise = 467},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR1, .SampleRate = 12524, .BinWidth = 25, .Bandwidth = 6262, .Noise = 260},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR2, .SampleRate = 1566, .BinWidth = 3.1, .Bandwidth = 783, .Noise = 100},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR3, .SampleRate = 196, .BinWidth = 0.38, .Bandwidth = 98, .Noise = 38}
   }

   ' Have also tried the below instead, with no luck
   Public Shared SampleRatings As New ReadOnlyCollection(Of SampleRating)(New List(Of SampleRating)() From {
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR0, .SampleRate = 100189, .BinWidth = 196, .Bandwidth = 26000, .Noise = 467},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR1, .SampleRate = 12524, .BinWidth = 25, .Bandwidth = 6262, .Noise = 260},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR2, .SampleRate = 1566, .BinWidth = 3.1, .Bandwidth = 783, .Noise = 100},
       New SampleRating With {.Setting = SampleRating.SampleRateSetting.SR3, .SampleRate = 196, .BinWidth = 0.38, .Bandwidth = 98, .Noise = 38}
    })

    Function Shared Bar() As Boolean
        ' Should NOT be able to change the list or list item members here.
    End Function
End Class 'End of Foo

如果你想确保列表和列表项都是只读的,你必须做两件事。

首先,您需要在 List 上使用 ReadOnlyCollection instead of a list. Basically, you create your list normally using a standard List object and once it's completed, you can call the AsReadOnly 方法来获取 ReadOnlyCollection。

您要做的第二件事是创建 read-only properties。根据您的需要,您还可以考虑为您的 属性 添加一个私有 setter... 根据定义,它不会是只读的 属性,而是 setter 不会暴露在您的 class 之外,因此它将防止任何其他对象更改您的 属性.

的值

更新

您提到不应从 Bar 函数修改 属性...这里的问题是您试图防止在 class 中对 SampleRatings 进行修改这也用于初始化 SampleRatings

请注意以下示例中的私有 setter。当它是私有的时,你不能初始化 SampleRatings,但是如果你使它成为 public,你就可以在 Bar 函数中改变它。

Public Class Foo
    Public Class SampleRating
        Private _noise As UInt16
        Public Property Noise() As UInt16
            Get
                Return _noise
            End Get
            Private Set(ByVal value As UInt16)
                _noise = value
            End Set
        End Property
    End Class

    'If the noise is read-only (or has a private setter), you won't be able to set the noise value here...
    Public Shared SampleRatings As New ReadOnlyCollection(Of SampleRating)(New List(Of SampleRating)() From {
        New SampleRating With {.Noise = 467},
        New SampleRating With {.Noise = 260},
        New SampleRating With {.Noise = 100},
        New SampleRating With {.Noise = 38}
     })

    Shared Function Bar()

        'Doesn't work
        SampleRatings.Remove(SampleRatings.First())

        'Doesn't work
        SampleRatings.First().Noise = 1

    End Function

End Class

您一开始就没有提到为什么要在您的列表中阻止修改...如果您想防止来自外部程序集的修改,您应该考虑将您的 setter 声明为 Friend(参见 Access Levels in Visual Basic)。