locked
Error remove lines RRS feed

  • Question

  • Hi All,

    Have 2 List  C_ and F_.Want to remove every line has same digits and leave only one.

     In list C_ , LIne 1 19 , Line 4 19, Line 5 91 leave one of 19.

    Line 6 76, Line 7 67, Line 8 76 then leave one 76 in list C_.

    I use this code but error if List C_ having same digit 19 line 1 and 19 line 4.same with digit 76.

    Public Class Form1
    
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            Dim G_ = New Dictionary(Of String, String)
            For i = 0 To C_.Count - 1
                G_.Add(C_(i), F_(i))
            Next
            For i = 0 To G_.Count - 1
                For j = 0 To G_.Count - 1
                    If G_.Values(i) = G_.Values(j) And i <> j Then
                        G_.Remove(G_.Keys(j))
                    End If
                Next
            Next
            For i = 0 To G_.Count - 1
                TextBox1.AppendText(G_.Keys(i) & "    " & G_.Values(i) & "     " & vbCrLf)
            Next
    
        End Sub
    End Class
    

    error message is;

    19    101     1
    99    203     2
    55    205     3
    19    101     4
    91    442     5
    76    505     6
    67    505     7
    76    663     8
    31    321     9

    and result expected as bellow

    19    101     
    99    203     
    55    205     
    76    505     
    31    321       
    How to do it in code

    thank

    Monday, June 11, 2018 1:12 AM

Answers

  • Hi mipakteh,

    Now I get your mean, you can take a look the following code:

      Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
            For i = 0 To C_.Count - 1
                For j = i + 1 To C_.Count - 2
                    If C_(i) = StrReverse(C_(j)) OrElse C_(i) = C_(j) And i <> j Then
                        C_(j) = ""
                        F_(j) = ""
                    End If
                Next
            Next
            C_.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
            F_.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
    
            For i = 0 To C_.Count - 1
                TextBox1.AppendText(C_(i) & "    " & F_(i) & "     " & vbCrLf)
            Next
    
        End Sub
    
        Public Shared Function IsNullOrWhiteSpace(value As String) As Boolean
            If value Is Nothing Then
                Return True
            End If
            For i As Integer = 0 To value.Length - 1
                If Not Char.IsWhiteSpace(value(i)) Then
                    Return False
                End If
            Next
            Return True
        End Function

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 11, 2018 9:36 AM
  • You are making it too complicated.  Use the code you already had, and follow the instructions provided.

    I can't follow what your matching rule currently is, so the test in the class might need to change.  The important point is that ONLY the test in the class IsEqual function changes when your rule changes - everything else stays the same.

    The code to select unique/not unique should be very familiar to you.

    Option Strict On
    Option Infer Off
    Option Explicit On
    
    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            Dim CoupleAB As New List(Of ItemInfo)
            For I As Integer = 0 To C_.Count - 1
                CoupleAB.Add(New ItemInfo With {.C_ = C_(I), .F_ = F_(I)})
                TextBox0.AppendText(CoupleAB(I).ToString & vbCrLf)
            Next
    
            For I As Integer = 0 To CoupleAB.Count - 1
                Dim Matched As Boolean = False
                For J As Integer = 0 To CoupleAB.Count - 1
                    If I <> J Then
                        If CoupleAB(I).IsEqual(CoupleAB(J)) Then
                            Matched = True
                            Exit For
                        End If
                    End If
                Next
                If Matched = True Then
                    TextBox1.AppendText(CoupleAB(I).ToString & vbCrLf)
                Else
                    TextBox2.AppendText(CoupleAB(I).ToString & vbCrLf)
                End If
            Next
        End Sub
    
    End Class
    
    Public Class ItemInfo
        Public C_ As String
        Public F_ As String
    
        Public Overrides Function ToString() As String
            Return C_.ToString & " => " & F_.ToString
        End Function
    
        ''' <summary>
        ''' Function to test for equality of class instances
        ''' Instances are equal IFF:
        '''   C_ values are equal, or
        '''   One C_ value is the reverse of the other.
        ''' </summary>
        ''' <param name="obj"></param>
        ''' <returns>True or False</returns>
        Public Function IsEqual(obj As ItemInfo) As Boolean
            If Me.C_ = obj.C_ Then Return True
            If Me.C_ = obj.C_.Reverse.ToString Then Return True
            Return False
        End Function
    End Class
    

    • Marked as answer by mipakteh Thursday, June 21, 2018 9:51 AM
    Tuesday, June 12, 2018 3:31 AM

All replies

  • Hi mipakteh,

    According to your data, I find that there are two 19 in column 1, the dictionary<Tkey, Tvalue> 's key must be unique, so if this column have some same data, you can not use dictionary<Tkey,Tvalue>, you can  just use list to do this.

    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            For i = 0 To F_.Count - 1
                For j = i + 1 To F_.Count - 2
                    If F_(i) = F_(j) And i <> j Then
                        F_.Remove(F_(j))
                        C_.Remove(C_(j))
                    End If
                Next
            Next
            For i = 0 To C_.Count - 1
                TextBox1.AppendText(C_(i) & "    " & F_(i) & "     " & vbCrLf)
            Next
    
        End Sub
    End Class
    
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 11, 2018 7:14 AM
  • Thank Cherry,

    but expected result is;

    19    101     
    99    203     
    55    205     
    76    505     
    31    321       

    that mean remove including the digit 91 and 67.

    Monday, June 11, 2018 8:05 AM
  • that mean remove including the digit 91 and 67.

    If you use a class for your objects, instead of 2 separate lists, then you can write the comparison for the class to do whatever you want.  For instance, you can write it so that '91' is treated as equal to both '91' and '19', or whatever equality you want.  That way you can write code like
        If ItemInfo(I).IsEqual(ItemInfo(J) Then ...
    and an item info that has property values of 19 and 101 will be 'equal' to another item info that has property values of 91 and 442.  This ability to define the behaviour of the class in any way you want is the big advantage of using classes.

    Monday, June 11, 2018 8:46 AM
  • Hi mipakteh,

    Now I get your mean, you can take a look the following code:

      Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
            For i = 0 To C_.Count - 1
                For j = i + 1 To C_.Count - 2
                    If C_(i) = StrReverse(C_(j)) OrElse C_(i) = C_(j) And i <> j Then
                        C_(j) = ""
                        F_(j) = ""
                    End If
                Next
            Next
            C_.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
            F_.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
    
            For i = 0 To C_.Count - 1
                TextBox1.AppendText(C_(i) & "    " & F_(i) & "     " & vbCrLf)
            Next
    
        End Sub
    
        Public Shared Function IsNullOrWhiteSpace(value As String) As Boolean
            If value Is Nothing Then
                Return True
            End If
            For i As Integer = 0 To value.Length - 1
                If Not Char.IsWhiteSpace(value(i)) Then
                    Return False
                End If
            Next
            Return True
        End Function

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 11, 2018 9:36 AM
  • Thank Cherry,

    but expected result is;

    19    101     
    99    203     
    55    205     
    76    505     
    31    321       

    that mean remove including the digit 91 and 67.

    Why? If it is based on a real digit then the result should be

    19 101

    and nothing more. Because all contain a 0 or 1

    If you are somehow hidden meaning that a reverse character string is also a digit. Yea what should I write .....


    Success
    Cor

    Monday, June 11, 2018 12:03 PM
  • Hi mipakteh

    I would be interested to know what it is that you are doing. It has been a long time now that you have been chasing that same dog up that same tree.

    An awful lot of strange character manipulations. What is the end result going to be?


    Regards Les, Livingston, Scotland

    Monday, June 11, 2018 12:30 PM
  • yes Acamar , now I Start with class.just trying what to do at begining.

    Public Class Form1
    
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            Dim CoupleAB As New List(Of ItemInfo)
            For I As Integer = 0 To C_.Count - 1
                CoupleAB.Add(New ItemInfo With {.C_ = C_(I), .F_ = F_(I)})
            Next
    
            For Each Element As ItemInfo In CoupleAB
                TextBox1.AppendText(Element.ToString & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
        End Sub
    End Class
    
    Public Class ItemInfo
        Public C_ As String
        Public F_ As Integer
    
        Public Overrides Function ToString() As String
            Return C_.ToString & " : " & F_.ToString
        End Function
    End Class

    Monday, June 11, 2018 1:47 PM
  • hehe...Leshay interest to learn vb, it make angry with the code...
    Monday, June 11, 2018 1:50 PM
  • hehe...Leshay interest to learn vb, it make angry with the code...

    Well that certainly clarifies your intentions!

    - Wayne

    Monday, June 11, 2018 5:32 PM
  • yes Acamar , now I Start with class.just trying what to do at beginning.

    Now write an IsEqual method for the class.   For instance, the method will return True if the C_ property for the method argument is the same as the C_ property of the item info being tested OR if the C_ property of the argument is equal to the reverse of the C_ property of the item info being tested (or whatever the rule now is).

    When you have an IsEqual function tested and working you can use it to test each item in the list.  If IsEqual for an item info is false for every other item in the list, then the item is unique. Or, if IsEqual is True for any other item in the list then the item info is not unique.

    Monday, June 11, 2018 10:13 PM
  • Hi Acamar,

    Object become true 2 time , that mean we have "19" is two.But how to created something to show the perfect result.

    Option Strict On
    Option Infer Off
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    Imports System.Text
    
    
    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            Dim CoupleAB As New List(Of IsEqual)
    
            For I As Integer = 0 To C_.Count - 1
                CoupleAB.Add(New IsEqual With {.C_ = C_(I), .F_ = F_(I)})
            Next
    
            For Each Element As IsEqual In CoupleAB
                TextBox1.AppendText(Element.ToString & vbCrLf)
    
                If Element.C_.Contains("19") Then
                    TextBox2.AppendText(Object.Equals(C_, F_) & vbCrLf)
                End If
    
            Next
        End Sub
    
    End Class
    
    
    Public Class IsEqual
        Public C_ As String
        Public F_ As String
    
        Public Overrides Function ToString() As String
            Return C_.ToString & " => " & F_.ToString
        End Function
    
        Public Overrides Function Equals(obj As Object) As Boolean
            Dim IsEqualObj As IsEqual = TryCast(obj, IsEqual)
            If IsEqualObj Is Nothing Then
                Return False
            Else
                Return C_.Equals(IsEqualObj.C_)
            End If
        End Function
    
        Public Overrides Function GetHashCode() As Integer
            Return Me.C_.GetHashCode()
        End Function
    End Class
    

    Tuesday, June 12, 2018 2:44 AM
  • You are making it too complicated.  Use the code you already had, and follow the instructions provided.

    I can't follow what your matching rule currently is, so the test in the class might need to change.  The important point is that ONLY the test in the class IsEqual function changes when your rule changes - everything else stays the same.

    The code to select unique/not unique should be very familiar to you.

    Option Strict On
    Option Infer Off
    Option Explicit On
    
    Public Class Form1
        Private C_ As New List(Of String)
        Private F_ As New List(Of String)
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            C_.Add("19")
            C_.Add("99")
            C_.Add("55")
            C_.Add("19")
            C_.Add("91")
            C_.Add("76")
            C_.Add("67")
            C_.Add("76")
            C_.Add("31")
    
            F_.Add("101")
            F_.Add("203")
            F_.Add("205")
            F_.Add("101")
            F_.Add("442")
            F_.Add("505")
            F_.Add("505")
            F_.Add("663")
            F_.Add("321")
    
            Dim CoupleAB As New List(Of ItemInfo)
            For I As Integer = 0 To C_.Count - 1
                CoupleAB.Add(New ItemInfo With {.C_ = C_(I), .F_ = F_(I)})
                TextBox0.AppendText(CoupleAB(I).ToString & vbCrLf)
            Next
    
            For I As Integer = 0 To CoupleAB.Count - 1
                Dim Matched As Boolean = False
                For J As Integer = 0 To CoupleAB.Count - 1
                    If I <> J Then
                        If CoupleAB(I).IsEqual(CoupleAB(J)) Then
                            Matched = True
                            Exit For
                        End If
                    End If
                Next
                If Matched = True Then
                    TextBox1.AppendText(CoupleAB(I).ToString & vbCrLf)
                Else
                    TextBox2.AppendText(CoupleAB(I).ToString & vbCrLf)
                End If
            Next
        End Sub
    
    End Class
    
    Public Class ItemInfo
        Public C_ As String
        Public F_ As String
    
        Public Overrides Function ToString() As String
            Return C_.ToString & " => " & F_.ToString
        End Function
    
        ''' <summary>
        ''' Function to test for equality of class instances
        ''' Instances are equal IFF:
        '''   C_ values are equal, or
        '''   One C_ value is the reverse of the other.
        ''' </summary>
        ''' <param name="obj"></param>
        ''' <returns>True or False</returns>
        Public Function IsEqual(obj As ItemInfo) As Boolean
            If Me.C_ = obj.C_ Then Return True
            If Me.C_ = obj.C_.Reverse.ToString Then Return True
            Return False
        End Function
    End Class
    

    • Marked as answer by mipakteh Thursday, June 21, 2018 9:51 AM
    Tuesday, June 12, 2018 3:31 AM
  • Thank you very much Acamar,

    Good to learning.

    Thursday, June 21, 2018 9:51 AM
  • Hi WayneKing,

    It simple ,I like VB.Sometimes  it very useful for work routine.

    At Office lot of Math Linear I using and K Factor calculation.Very very useful.

    VB programming in MS is the best.

    Thursday, June 21, 2018 10:03 AM