VB:全局数组'声明预期'

时间:2014-07-16 18:16:10

标签: vb.net

Module GlobalConstants
    Public strCDRack(0 To 2, 0 To 1) As String
        strCDRack(0, 0) = "Deftones"
        strCDRack(0, 1) = "Tool"
        strCDRack(0, 2) = "Disturbed"

        strCDRack(0, 0) = "Sticky Fingers"
        strCDRack(0, 1) = "Beggars Banquet"
        strCDRack(0, 2) = "Let It Bleed"


End Module
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = strCDRack(0, 1)
    End Sub
End Class

我想创建一个全局数组,其中所有按钮都可以访问它。但我不知道该怎么做?

1 个答案:

答案 0 :(得分:1)

Public Class Form1
    Friend strCDRack(0 To 2, 0 To 1) As String   ' just declaring

    ' the error message `declaration expected` is telling you all that
    ' can be done at this level is to declare variables

    Sub Form Load...
        ' code to initialize the array or DO anything
        ' must be in a procedure
        strCDRack(0, 0) = "Deftones"
        strCDRack(1, 0) = "Tool"
        strCDRack(2, 0) = "Disturbed"

        strCDRack(0, 1) = "Sticky Fingers"
        strCDRack(1, 1) = "Beggars Banquet"
        strCDRack(2, 1) = "Let It Bleed"

你还搞砸了文字的标记。您声明了数组(0 To 2, 0 To 1),但后来只使用了(0)作为第一个维度(0,1, 2 )。

请参阅Scope in Visual Basic