5 Replies - 365 Views - Last Post: 17 December 2015 - 07:33 AM Rate Topic: -----

#1 Kelvin Nyota  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

How to improve this code-stackoverflow

Posted 17 December 2015 - 06:50 AM

How can I improve this code to prevent stackoverflow?


The highlighted problem is here:

 foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)


The code is here


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Controls.Add(RichTextBox1)


        Using reader As New StreamReader("C:\Users\Acer\Desktop\Text Files\output10.txt")
            Do Until reader.EndOfStream
                Dim parts = reader.ReadLine().Split("|"c)

                If replacements.ContainsKey(parts(0)) Then
                    replacements(parts(0)).Add(parts(1))
                Else
                    Dim newWordList As New List(Of String)
                    newWordList.Add(parts(1))
                    replacements.Add(parts(0), newWordList)
                End If


            Loop
        End Using




        RichTextBox1.Text = "You gave a bad advice, irregardless of your intention. You provided a bad advice, irregardless of your intention. "
    End Sub

    Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
        If e.KeyCode = Keys.F1 Then
            nextCheckIndex = 0
            CheckForReplacementText()
        End If
    End Sub

    Public Sub CheckForReplacementText()

        If nextCheckIndex = replacements.Count Then

            MessageBox.Show("Check complete.")
        Else

            checkWord = replacements.Keys.ElementAt(nextCheckIndex)

            foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
        End If

        If foundIndex > -1 Then

            ReplaceMenu.Items.Clear()

            For Each replacement In replacements(checkWord)
                With ReplaceMenu.Items.Add(replacement, Nothing, Sub(sndr As Object, ea As EventArgs)
                                                                     RichTextBox1.SelectedText = kamau
                                                                     CheckForReplacementText()
                                                                 End Sub)

                End With
            Next
            ReplaceMenu.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.Selectionstart + RichTextBox1.SelectionLength))
        Else
            nextCheckIndex += 1
            CheckForReplacementText()
        End If

    End Sub

    Public Sub ReplaceMenu_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ReplaceMenu.ItemClicked

        Dim checkWord = replacements.Keys.ElementAt(nextCheckIndex)

        For Each replacement In replacements(checkWord)

            replacement = e.ClickedItem.Text
            kamau = replacement

        Next
    End Sub

   
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: How to improve this code-stackoverflow

#2 CrazyVito11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 17-December 15

Re: How to improve this code-stackoverflow

Posted 17 December 2015 - 06:54 AM

well
you can add a errorhandler. this will fix it for now.
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Controls.Add(RichTextBox1)


        Using reader As New StreamReader("C:\Users\Acer\Desktop\Text Files\output10.txt")
            Do Until reader.EndOfStream
                Dim parts = reader.ReadLine().Split("|"c)

                If replacements.ContainsKey(parts(0)) Then
                    replacements(parts(0)).Add(parts(1))
                Else
                    Dim newWordList As New List(Of String)
                    newWordList.Add(parts(1))
                    replacements.Add(parts(0), newWordList)
                End If


            Loop
        End Using




        RichTextBox1.Text = "You gave a bad advice, irregardless of your intention. You provided a bad advice, irregardless of your intention. "
    End Sub

    Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyUp
        If e.KeyCode = Keys.F1 Then
            nextCheckIndex = 0
            CheckForReplacementText()
        End If
    End Sub

    Public Sub CheckForReplacementText()
on error goto errhandler

        If nextCheckIndex = replacements.Count Then

            MessageBox.Show("Check complete.")
        Else

            checkWord = replacements.Keys.ElementAt(nextCheckIndex)

            foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBox1.TextLength, RichTextBoxFinds.WholeWord)
        End If

        If foundIndex > -1 Then

            ReplaceMenu.Items.Clear()

            For Each replacement In replacements(checkWord)
                With ReplaceMenu.Items.Add(replacement, Nothing, Sub(sndr As Object, ea As EventArgs)
                                                                     RichTextBox1.SelectedText = kamau
                                                                     CheckForReplacementText()
                                                                 End Sub)

                End With
            Next
            ReplaceMenu.Show(RichTextBox1, RichTextBox1.GetPositionFromCharIndex(RichTextBox1.Selectionstart + RichTextBox1.SelectionLength))
        Else
            nextCheckIndex += 1
            CheckForReplacementText()
        End If
exit sub
errhandler:
msgbox "A error occurred! # " & err.number, MsgBoxStyle.Critical,"Error"
    End Sub

    Public Sub ReplaceMenu_ItemClicked(ByVal sender As Object, ByVal e As ToolStripItemClickedEventArgs) Handles ReplaceMenu.ItemClicked

        Dim checkWord = replacements.Keys.ElementAt(nextCheckIndex)

        For Each replacement In replacements(checkWord)

            replacement = e.ClickedItem.Text
            kamau = replacement

        Next
    End Sub

   
End Class


Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: How to improve this code-stackoverflow

Posted 17 December 2015 - 06:57 AM

We should not encourage the use of On Error GoTo which is a hangover from VB6, use proper structured error handling with Try..Catch.

But adding error handling doesn't fix the problem.

This post has been edited by andrewsw: 17 December 2015 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 Kelvin Nyota  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

Re: How to improve this code-stackoverflow

Posted 17 December 2015 - 07:02 AM

OK, can you help with that please?

I feel something wrong about the structure also.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: How to improve this code-stackoverflow

Posted 17 December 2015 - 07:26 AM

I'll explain the essential problem. If you replace "Tom" with "Tomato" then repeat this you get "Tomatoato"; if you repeat this process recursively, as you are, you get a stack overflow.

Personally, I would start over, first with some pseudo-code to describe the solution. If you want help with this then I suggest you describe clearly what is required. Describe the task.

This is my suggestion, but maybe someone else will assist you with your current code.

This post has been edited by andrewsw: 17 December 2015 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 Kelvin Nyota  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

Re: How to improve this code-stackoverflow

Posted 17 December 2015 - 07:33 AM

I would like to write it, but for now am very tired to even think clearly on how to write it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1