Anthony
Anthony

Reputation: 742

vb6 function not returning a value

VB noob here, working on a legacy VB 6.0 application.

When I check the value of lineno within the function below, I am getting the expected value:

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

But when I call GetNumOfLines from GetATRNames (below), numOfLines is 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

Any ideas on why numOfLines = GetNumOfLines(filename) is giving me a different value than when I check within GetNumOfLines?

Upvotes: 0

Views: 800

Answers (3)

JSR
JSR

Reputation: 6396

You need to return the value from your GetNumOfLines function

Add the line

GetNumOfLines = lineno

as the last line of your function.

Upvotes: 3

tcarvin
tcarvin

Reputation: 10855

You just need to return your value:

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

Upvotes: 3

user1945782
user1945782

Reputation:

You're not returning the value. Put:

GetNumOfLines = lineno

At the end of the first function.

Upvotes: 6

Related Questions