0

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?

3 Answers 3

6

You're not returning the value. Put:

GetNumOfLines = lineno

At the end of the first function.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

You need to return the value from your GetNumOfLines function

Add the line

GetNumOfLines = lineno

as the last line of your function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.