String Array Expecting Declaration?

时间:2014-03-14 16:09:41

标签: .net arrays vb.net class member-initialization

我在这个简单的代码中遇到了以下问题:

Public Class BookStoreDatabase

Public publicationArray(0 To 3) As String

publicationArray(0) = "Stories to Scare People With"
End Class

bit" publicationArray(0)等"告诉我," publicationArray"是期待。这似乎不应该发生。

1 个答案:

答案 0 :(得分:2)

您无法在Class级别分配数组元素。如果您需要在实例化BookStoreDatabase类本身后立即分配它,则必须使用构造函数:

Public Class BookStoreDatabase

    Public Sub New()
        publicationArray(0) = "Stories to Scare People With"
    End Sub

    Public publicationArray(0 To 3) As String

End Class