dcsimg
Results 1 to 14 of 14
  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Post Comparator does not throw exception again

    I am writing this comparator program and it run the first time and clicked the button and threw an index out of RangeException but now it does not even throw the exception again

    This is the code


    'This code is inside a Module
    Code:
    Module SaComparator
        Public Alist As New List(Of String)
        Public Sub SortA(ByVal NewWord As String)
    
            If Alist.Count <= 0 Then
                Alist.Add(NewWord)
                Exit Sub
    
            Else
                Alist.Add(NewWord)
                Dim FcomPosition As Integer
                Dim SComPosition As Integer
    
                'Start the main loop
                For a As Integer = 0 To Alist.Count - 1
                    While a < Alist.Count
                        Dim FrontCompared() As Char = Alist(a).ToCharArray
                        Dim NextCompared() As Char = Alist(a + 1).ToCharArray
    
    
                        FcomPosition = a
                        SComPosition = a + 1
    
    
    
                        For b As Integer = 0 To FrontCompared.Count - 1
                            If AlphaNumeric(AlphaDeterminer(FrontCompared(b)), AlphaDeterminer(NextCompared(b))) = 0 Then
                                Continue For
                            ElseIf AlphaNumeric(AlphaDeterminer(FrontCompared(b)), AlphaDeterminer(NextCompared(b))) = -1 Then
                                Exit For
                            ElseIf AlphaNumeric(AlphaDeterminer(FrontCompared(b)), AlphaDeterminer(NextCompared(b))) = 1 Then
                                Alist.RemoveAt(FcomPosition)
                                Alist.Insert(a + 1, NewWord)
                                Exit For
                            End If
    
                        Next
    
                    End While
                Next
            End If
           
    
    
        End Sub
    
        Private Function AlphaDeterminer(ByVal character As String) As Integer
            Select Case character
                Case "a"
                    Return 1
                Case "b"
                    Return 2
                Case "c"
                    Return 3
                Case "d"
                    Return 4
                Case "e"
                    Return 5
                Case "f"
                    Return 6
                Case "g"
                    Return 7
                Case "h"
                    Return 8
                Case "i"
                    Return 9
                Case "j"
                    Return 10
                Case "k"
                    Return 11
                Case "l"
                    Return 12
                Case "m"
                    Return 13
                Case "n"
                    Return 14
                Case "o"
                    Return 15
                Case "p"
                    Return 16
                Case "q"
                    Return 17
                Case "r"
                    Return 18
                Case "s"
                    Return 19
                Case "t"
                    Return 20
                Case "u"
                    Return 21
                Case "v"
                    Return 22
                Case "w"
                    Return 23
                Case "x"
                    Return 24
                Case "y"
                    Return 25
                Case "z"
                    Return 26
                Case Else
                    Return 0
            End Select
        End Function
    
        Private Function AlphaNumeric(ByVal Fval As Integer, ByVal Sval As Integer) As Integer
            If Fval = Sval Then
                Return 0
            ElseIf Fval < Sval Then
                Return -1
            Else
                Return 1
            End If
        End Function
    
    
    End Module

    'This code is inside the startup form
    Code:
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Alist.Add("mat")
            Alist.Add("pit")
            Alist.Add("rat")
            Alist.Add("cat")
            Alist.Add("sit")
            Alist.Add("pen")
            Alist.Add("see")
            Alist.Add("sum")
            Alist.Add("kin")
    
        End Sub
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SortA(TextBox1.Text)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For a As Integer = 0 To Alist.Count - 1
                ListBox1.Items.Add(Alist(a))
            Next
    
        End Sub
    End Class
    Nothing I post is Self Reliable. Use it at your own risk

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,516

    Re: Comparator does not throw exception again

    SortA sub is enter an infinite loop!

    Why you are trying to invent the wheel, you can sort List(Of String) using your own sorter, try this

    Add MyStringComparer class to your project

    Code:
    Public Class MyStringComparer
        Implements IComparer(Of String)
    
        Public Property SortOrder As SortOrder
    
        Public Sub New()
            Me.SortOrder = SortOrder.Ascending ' Set default sort order
        End Sub
    
        Public Sub New(eSortOrder As SortOrder)
            Me.SortOrder = eSortOrder
        End Sub
    
        Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
            ' Normal sort criteria
            If Me.SortOrder = SortOrder.Ascending Then
                Return x.CompareTo(y)
            ElseIf Me.SortOrder = SortOrder.Descending Then
                Return y.CompareTo(x)
            Else ' SortOrder.None
                Return 0
            End If
    
    
            '' Specific sort criteria, e.g. don't sort word start with minus sign
            'If x.StartsWith("-") Then
            '    Return 0
            'Else
            '    If Me.SortOrder = SortOrder.Ascending Then
            '        Return x.CompareTo(y)
            '    ElseIf Me.SortOrder = SortOrder.Descending Then
            '        Return y.CompareTo(x)
            '    Else ' SortOrder.None
            '        Return 0
            '    End If
            'End If
        End Function
    End Class
    Replace your module with this one
    Code:
    Module SaComparator
    
        Public Alist As New List(Of String)
    
        Public Sub SortA(ByVal NewWord As String)
            Alist.Add(NewWord)
            'Sort Ascending
            Alist.Sort(New MyStringComparer()) 'Default sort order is Ascending
            ''Sort Descending
            'Alist.Sort(New MyStringComparer() With {.SortOrder = SortOrder.Descending})
        End Sub
    End Module
    That's all.
    Last edited by 4x2y; Jan 22nd, 2018 at 03:06 PM.



  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by 4x2y View Post
    SortA sub is enter an infinite loop!

    Why you are trying to invent the wheel, you can sort List(Of String) using your own sorter, try this

    Add MyStringComparer class to your project

    Code:
    Public Class MyStringComparer
        Implements IComparer(Of String)
    
        Public Property SortOrder As SortOrder
    
        Public Sub New()
            Me.SortOrder = SortOrder.Ascending ' Set default sort order
        End Sub
    
        Public Sub New(eSortOrder As SortOrder)
            Me.SortOrder = eSortOrder
        End Sub
    
        Public Function Compare(x As String, y As String) As Integer Implements IComparer(Of String).Compare
            ' Normal sort criteria
            If Me.SortOrder = SortOrder.Ascending Then
                Return x.CompareTo(y)
            ElseIf Me.SortOrder = SortOrder.Descending Then
                Return y.CompareTo(x)
            Else ' SortOrder.None
                Return 0
            End If
    
    
            '' Specific sort criteria, e.g. don't sort word start with minus sign
            'If x.StartsWith("-") Then
            '    Return 0
            'Else
            '    If Me.SortOrder = SortOrder.Ascending Then
            '        Return x.CompareTo(y)
            '    ElseIf Me.SortOrder = SortOrder.Descending Then
            '        Return y.CompareTo(x)
            '    Else ' SortOrder.None
            '        Return 0
            '    End If
            'End If
        End Function
    End Class
    Replace your module with this one
    Code:
    Module SaComparator
    
        Public Alist As New List(Of String)
    
        Public Sub SortA(ByVal NewWord As String)
            Alist.Add(NewWord)
            'Sort Ascending
            Alist.Sort(New MyStringComparer()) 'Default sort order is Ascending
            ''Sort Descending
            'Alist.Sort(New MyStringComparer() With {.SortOrder = SortOrder.Descending})
        End Sub
    End Module
    That's all.
    I am not using the visual basic ICompare interface. I am creating my own custom comparer.
    Nothing I post is Self Reliable. Use it at your own risk

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by 4x2y View Post
    SortA sub is enter an infinite loop!

    Why you are trying to invent the wheel
    How is it entering an infinite loop?
    Nothing I post is Self Reliable. Use it at your own risk

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,606

    Re: Comparator does not throw exception again

    Quote Originally Posted by TATARPRO View Post
    I am not using the visual basic ICompare interface. I am creating my own custom comparer.
    Right... that's what the ICompare interface is for... implementing your own custom sorts.

    Quote Originally Posted by TATARPRO View Post
    How is it entering an infinite loop?
    It's your While a < Alist.Count loop.... a is never incremented or changed, so once it goes in there, it'll never come back out.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by techgnome View Post
    It's your While a < Alist.Count loop.... a is never incremented or changed, so once it goes in there, it'll never come back out.

    -tg
    Oh my God! That was difficult to notice. thanks a lot. that's the same result I get when I pause the program. I keep seeing it repeating that block of code.
    Nothing I post is Self Reliable. Use it at your own risk

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by techgnome View Post
    Right... that's what the ICompare interface is for... implementing your own custom sorts.

    -tg
    That's not what I mean. I mean I want to write a comparer class.
    Nothing I post is Self Reliable. Use it at your own risk

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,606

    Re: Comparator does not throw exception again

    Quote Originally Posted by TATARPRO View Post
    That's not what I mean. I mean I want to write a comparer class.
    Right.... so you create a class.... it Implements IComparer .... you fill in the code to do the compare and return results... then use it to sort your list, array, whatever. Again... that's what it's for.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,516

    Re: Comparator does not throw exception again

    Quote Originally Posted by TATARPRO View Post
    That's not what I mean. I mean I want to write a comparer class.
    I have provided an example how to do specific sort in the function Compare(x As String, y As String) see the commented lines!

    What exactly kind of sort you want to do? Why you are compare two words character by character?



  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by techgnome View Post
    Right.... so you create a class.... it Implements IComparer .... you fill in the code to do the compare and return results... then use it to sort your list, array, whatever. Again... that's what it's for.

    -tg
    may be I'm saying compare so you are getting confused . I mean I want to write a class to sort strings just like visual basic has the .sort() method. not using visual basic sort method instead.
    Nothing I post is Self Reliable. Use it at your own risk

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    225

    Re: Comparator does not throw exception again

    Quote Originally Posted by 4x2y View Post
    Why you are compare two words character by character?
    compare two words character by character because that's the only way to know that 'word A' is to come before 'word B'.
    Nothing I post is Self Reliable. Use it at your own risk

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,606

    Re: Comparator does not throw exception again

    Quote Originally Posted by TATARPRO View Post
    may be I'm saying compare so you are getting confused . I mean I want to write a class to sort strings just like visual basic has the .sort() method. not using visual basic sort method instead.
    Ah... I think we're talking cross-purpose here... That's how .Sort works ... it invokes the IComparer of what ever class is being sorted (string, custom class, what ever) ... the comparer class then compares the two values, and returns -1 if value1 is less than value2, 0 if value1 equals valule2, or 1 if value1 is greater than value2 ... based on that comparison return value, it will then either swap the values, or not... The swapping of values is done for you, all you need to do is implement the IComparer logic to return -1,0,1 as necessary.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,516

    Re: Comparator does not throw exception again

    Quote Originally Posted by TATARPRO View Post
    compare two words character by character because that's the only way to know that 'word A' is to come before 'word B'.
    There are many ways to know that

    A simple comparison
    Code:
            Dim a As String = "abc"
            Dim b As String = "abd"
            If a = b Then
                MessageBox.Show(Me, "a = b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf a > b Then
                MessageBox.Show(Me, "a come after b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf a < b Then
                MessageBox.Show(Me, "a come before b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
    Using String.Compare method
    Code:
            Dim a As String = "abc"
            Dim b As String = "abd"
            Dim c As Integer = String.Compare(a, b)
    
            If c = 0 Then
                MessageBox.Show(Me, "a = b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf c = 1 Then
                MessageBox.Show(Me, "a come after b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            ElseIf c = -1 Then
                MessageBox.Show(Me, "a come before b", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If



  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,029

    Re: Comparator does not throw exception again

    If the reason your are doing this is just for experimentation or practice, then all these comments are irrelevant. However, if you are trying to do comparisons or sorting for some reason other than just for experimentation or practice, you aren't going to do it better than with the tools and methods that MS is providing for you.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width