user3340949 user3340949 - 1 month ago 11
Vb.net Question

Find all instances from text file and return values

I have this code for finding a value in a text file and then reading another value in the same line. What I have since found out from the large text file I am reading is that there can be two or more lines with the initial value I am seraching for, which is a part number. After finding the part number I am getting the quantity. How can I modify this to find ALL of the part number lines and then return each value from the line?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fp as string = "" 'enter the full path to your file here
Dim value as string = GetValueForPart(fp, Me.TextBox1.Text)
MsgBox(value) 'in this example, value is set to "6.237" when textbox input is "TR2999-01G"
End Sub

Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String
If Not File.Exists(filepath) Then Return Nothing
If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
Dim ret As String = Nothing
Using sr As New StreamReader(filepath)
Do While sr.Peek >= 0
Dim line() As String = sr.ReadLine.Split(CChar("|"))
If line IsNot Nothing AndAlso line.Count >= 5 Then
If line(1).Equals(SearchPartNum) Then
ret = line(9)
Exit Do
End If
End If
Loop
End Using
Return ret
End Function

Answer

You just need to change the loop to keep a running sum of your quantities, instead of just finding a single quantity and returning it. Each modification has a comment by it in the code below, basically we're just changing the function to return a quantity sum instead of a string. For each line, if the search is found, the loop will add the quantity value to a sum that it's keeping. After all lines are processed, it will return the sum of the quantities.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fp As String = ""
    'Dim value As String = GetValueForPart(fp, Me.TextBox1.Text)
    Dim value As Double = GetValueForPart(fp, Me.TextBox1.Text) 'need to change this to accept a double return
    MsgBox(value.ToString) 'add conversion to string just for display
End Sub

Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As Double 'change this to return double
    If Not File.Exists(filepath) Then Return Nothing
    If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
    'Dim ret As String = Nothing
    Dim qtySum As Double = 0 'change this to keep a running sum of the quantities returned
    Using sr As New StreamReader(filepath)
        Do While sr.Peek >= 0
            Dim line() As String = sr.ReadLine.Split(CChar("|"))
            If line IsNot Nothing AndAlso line.Count >= 5 Then
                If line(1).Equals(SearchPartNum) Then
                    'ret = line(9)
                    'Exit Do
                    'Instead of finding a quantity and exiting, convert each quantity found
                    'into a double and add it to our running sum
                    qtySum += Convert.ToDouble(line(9))
                End If
            End If
        Loop
    End Using
    'Return ret
    Return qtySum
End Function
Comments