0

这里是 VB 菜鸟,正在处理旧版 VB 6.0 应用程序。

当我在下面的函数中检查 lineno 的值时,我得到了预期值:

Public Function GetNumOfLines(filename As String) As Integer
    Dim lineno as Integer
    lineno = 0  
    Open App.Path + filename For Input As #1

    Do While Not EOF(1)
        lineno = lineno + 1
        Line Input #1, linevar
        Loop
        Close #1

    MsgBox "numOfLines: " & lineno 'This works
    End Function

但是当我从 GetATRNames(如下)调用 GetNumOfLines 时,numOfLines 为 0:

Public Function GetATRNames() As String()   
    Dim filename as String  
    filename = "\atrname.dat"
    Dim numOfLines as Integer
    numOfLines = GetNumOfLines(filename)

    MsgBox "numOfLines: " & numOfLines 'This does not
        End Function

关于为什么 numOfLines = GetNumOfLines(filename) 给我的值与我在 GetNumOfLines 中检查时不同的任何想法?

4

3 回答 3

6

你没有返回值。放:

GetNumOfLines = lineno

在第一个函数结束时。

于 2013-03-19T16:55:48.173 回答
3

您只需要返回您的值:

Public Function GetNumOfLines(filename As String) As Integer
    Dim lineno as Integer
    lineno = 0  
    Open App.Path + filename For Input As #1

    Do While Not EOF(1)
        lineno = lineno + 1
        Line Input #1, linevar
        Loop
        Close #1

    MsgBox "numOfLines: " & lineno 'This works

    'return number of lines
    GetNumOfLines = lineno

    End Function
于 2013-03-19T16:56:11.207 回答
3

您需要从 GetNumOfLines 函数返回值

添加行

GetNumOfLines = lineno

作为函数的最后一行。

于 2013-03-19T16:57:10.927 回答