江湖的大佬们,我是个新手,我想定义一个 当前窗体中所有sub或function可以使用的 array.
我看了网上说只要定义到模块中就能用了,但是我这搞死都提示:下标越界。。
请指教!!。感激不尽啊。。
模块中:
Public my_arr() As SinglePublic Function get_array()
    Dim fnr As Integer
    Dim arr_len As Integer
    Dim another As String
    Dim my_arr()
    fnr = FreeFile
    arr_len = 0
    filter_line = getlinescount("filter.txt") - 1
    If filter_line < 0 Then
        filter_line = 0
    End If
    ReDim my_arr(filter_line)
    Open "filter.txt" For Input As #fnr
    If LOF(fnr) = 0 Then
        MsgBox "请在右侧输入最少一对开始和结束字符!"
        Close #fnr
    Else
        Do While Not EOF(1)
        Input #1, another
        my_arr(arr_len) = another
        arr_len = arr_len + 1
        Loop
        Close #fnr
    End If
    
    MsgBox my_arr(0)  ---这里是可以取到值的
End Function 【窗体中代码:】Private Sub btnCompute_Click()
    If Dir("output.txt") <> "" Then
        Kill "output.txt"
        Open "output.txt" For Output As #1
        Close #1
    End If
    btnCompute.Enabled = False
    Dim s As String
    Dim n, filepath
    
    filepath = txtfp.Text
    If filepath = "" Then
        MsgBox "请选择文件路径!"
    Else
        If chkIsfilt.Value = 1 Then
            Call filter_to_new(filepath)
            Call get_array
            MsgBox my_arr(0)   ----搞死提示:下标越界求救啊
        End If
    End If
    btnCompute.Enabled = True
End Sub

解决方案 »

  1.   

    Public my_arr() As SinglePublic Function get_array()
      Dim fnr As Integer
      Dim arr_len As Integer
      Dim another As String
      Dim my_arr()
    ==================================你在function中又重新定义了一个同名的数组, 按照优先规则,你在function中对my_arr所做的任何动作只是对于其内部的数组生效, 而对那个公有的public my_arr()没有任何作用.当你在另外的过程btnCompute_Click中访问的是public my_arr(), 这个数组既没有初始化,更没有赋值, 当然越界咯.
      

  2.   

    Const MAXN=10000
    Public global_arr(MAXN) As Single
      

  3.   

    谢谢楼上两位的回复。
    回:zhao4zhong1.我这个数组预先是不知道大小的。所以先不能定位const
    回:WallesCai. 我按照你的意思改了。但是还是不行,。求救啊
    Public my_arr() As StringPublic Function get_array()
        Dim fnr As Integer
        Dim arr_len As Integer
        Dim another As String
        Dim my_arr()
        fnr = FreeFile
        arr_len = 0
        filter_line = getlinescount("filter.txt") - 1
        If filter_line < 0 Then
            filter_line = 0
        End If
        ReDim my_arr(filter_line)
        Open "filter.txt" For Input As #fnr
        If LOF(fnr) = 0 Then
            MsgBox "请在右侧输入最少一对开始和结束字符!"
            Close #fnr
        Else
            Do While Not EOF(1)
            Input #1, another
            my_arr(arr_len) = another
            arr_len = arr_len + 1
            Loop
            Close #fnr
        End If
        
        MsgBox my_arr(0)  --module 中可以取到值
    End Function
    窗体代码:Private Sub btnCompute_Click()
        If Dir("output.txt") <> "" Then
            Kill "output.txt"
            Open "output.txt" For Output As #1
            Close #1
        End If
        btnCompute.Enabled = False
        Dim s As String
        Dim n, filepath
        
        Dim my_arr()   -定义一下
        fnr = FreeFile
        arr_len = 0
        filter_line = getlinescount("filter.txt") - 1
        If filter_line < 0 Then
            filter_line = 0
        End If
        ReDim my_arr(filter_line)  -初始化下大小。
        
        filepath = txtfp.Text
        If filepath = "" Then
            MsgBox "请选择文件路径!"
        Else
            If chkIsfilt.Value = 1 Then
                Call filter_to_new(filepath)
                Call get_array
                MsgBox my_arr(0)  -----------还是不行啊。取到空。
            End If
        End If
        btnCompute.Enabled = True
    End Sub