none
Move to the next personal ID if the function = true RRS feed

  • Question

  • i have file text to personals ID okay i wana move to next personal id if function = true

    this is my try 

        Dim enumerator As List(Of String).Enumerator
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim ListUsers As List(Of String)
            ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
    
            enumerator = ListUsers.GetEnumerator()
    
            While True
                Dim current As String = enumerator.Current
                CheckId(current)
            End While
    
        End Sub
        Function CheckId(id As String)
            If id.Contains("ID:") Then
                enumerator.MoveNext()
                Return True
            Else
                Return False
            End If
        End Function

    but its no work 

    Wednesday, January 22, 2020 10:35 AM

All replies

  • Seems you should be able to use the following

    Dim ListUsers As List(Of String)
    ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
    
    Dim enumerator As IEnumerator = ListUsers.GetEnumerator()
    
    Do While enumerator.MoveNext() AndAlso enumerator.Current.ToString().Contains("ID")
        Dim item As Object = enumerator.Current
        Console.WriteLine(item)
    Loop

    Or

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
        Dim enumerator As IEnumerator = ListUsers.GetEnumerator()
    
        Do While enumerator.MoveNext() AndAlso CheckId(enumerator)
            Dim item As Object = enumerator.Current
            Console.WriteLine(item)
        Loop
    
    End Sub
    Function CheckId(enumerator As IEnumerator) As Boolean
    
        If enumerator.Current.ToString().Contains("ID") Then
            Return True
        Else
            Return False
        End If
    End Function

    Or

    Dim ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
    For Each item As String In ListUsers
        If Not item.Contains("ID") Then
            Exit For
        End If
        Console.WriteLine(item)
    Next

    Or

    Dim ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
    Dim results = ListUsers.Where(Function(line) line.Contains("ID")).ToList()
    For Each line As String In results
        Console.WriteLine(line)
    Next

    Or

    Dim ListUsers = New List(Of String)(File.ReadAllLines("list.txt"))
    Dim results = ListUsers.Select(Function(line, index) New With {
                                      .Line = line,
                                      .Index = index + 1}).
            Where(Function(item) item.Line.Contains("ID"))
    
    For Each line In results
        Console.WriteLine($"{line.Index} - {line.Line}")
    Next
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Wednesday, January 22, 2020 12:01 PM
    Moderator