none
Comparing Lines Using Loop RRS feed

  • Question

  • Hi All,

    I have value for every lines in List (Of T).

    How the way to find the same value for the whole lines.

    The code bellow not working.Why..

    somebody can correct.

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.IO
    Imports System.Data
    
    Public Class Form1
        Dim ETD As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf _
                  & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
                  & "1, 1, 1, 1, 1, 1, 2, 2," & vbCrLf _
                  & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,3 ,4 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,2 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,4 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf _
    & "1 ,1 ,2 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,3 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf _
    & "1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
    
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            For I As Integer = 0 To ETD.Count - 1
                TextBox1.AppendText(ETD(I))
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            For I As Integer = 0 To ETD.Count - 2
                TextBox2.AppendText("Comparing Line " & I.ToString & " : " & ETD(I).ToString & vbCrLf)
                For J As Integer = I + 1 To ETD.Count - 1
                    TextBox2.AppendText("To           " & J.ToString & " : " & ETD(J).ToString & vbCrLf)
                    If ETD(I).ToString = ETD(J).ToString Then
                        TextBox2.AppendText(ETD(J).ToString & vbCrLf)
                    End If
                Next
            Next
    
        End Sub
    End Class
    

    Sunday, September 17, 2017 5:56 AM

Answers

  • The code bellow not working.Why..

    What do you mean by 'not working'?  What did you expect to happen, and what actually happened?

    You should use the debugger and insert a breakpoint at the start of the loop, and single step the code around the loop.  In particular you need to see what it getting compared each time.  That should identify the proble, quite quickly.

    • Marked as answer by mipakteh Sunday, September 17, 2017 11:06 AM
    Sunday, September 17, 2017 6:57 AM

All replies

  • The code bellow not working.Why..

    What do you mean by 'not working'?  What did you expect to happen, and what actually happened?

    You should use the debugger and insert a breakpoint at the start of the loop, and single step the code around the loop.  In particular you need to see what it getting compared each time.  That should identify the proble, quite quickly.

    • Marked as answer by mipakteh Sunday, September 17, 2017 11:06 AM
    Sunday, September 17, 2017 6:57 AM
  • HI Acamar, When push Button_2,Nothing happen.It may be something wrong about List. I want to check for my past question .Avoid from repeated comparing lines.
    Sunday, September 17, 2017 7:22 AM
  • When push Button_2,Nothing happen.

    That's what happened - what do you think should have happened?

    What was the result when you inserted the breakpoint, and single-stepped the code through the loop?  How many times did t go around?  Why that many?

    Sunday, September 17, 2017 7:55 AM
  • yup , by changing Add to the List every line it Button_2 working.

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1, 1, 1, 1, 1, 1, 2, 2," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,3 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,2 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,4 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,1 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,2 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,3 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,2 ,2 ,3 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,1 ,1 ,2 ,2 ," & vbCrLf)
            ETD.Add("1 ,1 ,1 ,1 ,2 ,2 ,2 ,")
    
        End Sub

    But I can't think how to avoid repeated comparing every lines..

    so help me Acamar.

    see at line 1 ,1 ,1 ,2 ,2 ,3 ,

    Sunday, September 17, 2017 8:46 AM
  • see at line 1 ,1 ,1 ,2 ,2 ,3 ,

    If your problem has been solved then you should mark an answer and ask a new question.  When you ask a new question I would recommend:

    - Provide an example of suitable size.  It should be big enough to cover the required cases, but no bigger.
    - Show the result that you expect to get.   You have described what is happening, but there is no indication of what should happen.   Your code is carefully set up to compare every pair of items in the list, but your comment indicates that's not what you want to do!

    Sunday, September 17, 2017 9:50 AM