vb6函数没有返回值

时间:2013-03-19 16:53:22

标签: vb6

VB noob,处理遗留的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中检查时的值?

3 个答案:

答案 0 :(得分:6)

您没有返回该值。放:

GetNumOfLines = lineno

在第一个功能结束时。

答案 1 :(得分: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

答案 2 :(得分:3)

您需要从GetNumOfLines函数返回值

添加行

GetNumOfLines = lineno

作为你职能的最后一行。