Results 1 to 2 of 2

Thread: Need help fixing project

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2016
    Posts
    3

    Question Need help fixing project

    So on my program it is supposed to find the word positions and place them in a text box example: Hi i am bdmaurice and i am new would be 1 2 3 4 5 2 3 6.

    This is my code so far i just needed a bit of help to make it work.

    Code:
    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.Menu
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox2.Text = ""
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            Dim ArrayX() As String = Split(TextBox1.Text, " ")
            Dim Number As Integer = 0
            Dim List As New Dictionary(Of String, String)
            Dim item As String
            For Each item In ArrayX
    
            Next
            If Not List.ContainsKey(item) Then
                Number = (Number + 1)
                List.Add(item, Number)
            End If
            'displays the numbers for each word in a list
            ListBox2.Items.Add((List(Item)))
            'displays the dictionary list of items with their assigned numbers
            For Each item In List(item)
                ListBox1.Items.Add(item.ToString.Replace("[", "").Replace("]", ""))
            Next
            'adds all the numbers to textbox2 and adds spaces after each
            For Each item In ListBox2.Items
                TextBox2.Text = TextBox2.Text & (item) & " "
            Next
            'converts the numbers into their given words in textbox2 from it's number
            TextBox2.Text = String.Join("|", Array.ConvertAll(TextBox2.Text.ToArray, Function(c) If(List.Keys.Contains(c.ToString), List(c.ToString), c.ToString)))
            TextBox2.Text = String.Concat(Array.ConvertAll(TextBox2.Text.Split("|"c), Function(s) If(List.Values.Contains(s), List.First(Function(kvp) kvp.Value = s).Key, s)))
        End Sub
    
    
    
    
        Private Function Item() As String
            Throw New NotImplementedException
        End Function
    
    End Class
    And in the text box it says: new and then in both list boxes it says: 1

    Thanks!
    Last edited by dday9; Oct 18th, 2016 at 08:10 AM. Reason: Added Code Tags

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Need help fixing project

    Ok, let us step through your code. Your first 3 lines in the Click event seem to be clearing existing data:
    Code:
    'Clear existing data
    TextBox2.Text = ""
    ListBox1.Items.Clear()
    ListBox2.Items.Clear()
    Next it looks like you're getting all the words in TextBox1 by separating the Text by a single space:
    Code:
    Dim ArrayX() As String = Split(TextBox1.Text, " ")
    Only you should replace the legacy method Split with String.Split:
    Code:
    'Get each word in TextBox separated by a single space
    Dim ArrayX() As String = TextBox1.Text.Split(" "c)
    Then it looks like you're declaring some variables for later in the code:
    Code:
    Dim Number As Integer = 0
    Dim List As New Dictionary(Of String, String)
    Dim item As String
    Next it looks like you're looping through each item in the array, but you aren't doing anything. This first For/Each loop should be removed:
    Code:
    For Each item In ArrayX
    
    Next
    Then it you're checking if your Dictionary contains a key which is declared. The issue with this is three-fold:
    1. Your Dictionary doesn't have any KeyValue pairs to check if there is an item that contains the key
    2. Your key that you're checking if it is contained in the Dictionary is a blank String, it was never assigned a value
    3. If the code would have executed, you cannot have 2 KeyValuePairs in a Dictionary that have the same key. So when you add the new item to the Dictionary an exception would be thrown.

    Code:
    'Check if the Item is a key in the Dictionary
    If Not List.ContainsKey(item) Then
        'If so, then increment the number variable by 1 and add the Key and the Number to the Dictionary
        Number += 1
        List.Add(item, Number)
    End If
    Next, you try to Add the Value from the KeyValuePair in the Dictionary where the Key equals the declared variable Item, but... you'd run into the same first two issues as when you try to run ContainsKey only this time an exception should get thrown.
    Code:
    'displays the numbers for each word in a list
    ListBox2.Items.Add((List(Item)))
    Then it looks like you are trying to loop through each item in the Dictionary who's key equals the declared variable Item, add the declared variable Item to a ListBox, but first remove any [ and ] characters. The issue with this is that you cannot have more than 1 item in a Dictionary that has the same key:
    Code:
    'displays the dictionary list of items with their assigned numbers
    For Each item In List(item)
         ListBox1.Items.Add(item.ToString.Replace("[", "").Replace("]", ""))
    Next
    Next it looks like you want to concatenate all the items in a ListBox to a TextBox, however I'd use the method String.Join for this to avoid looping:
    Code:
    'Convert the ListBox items to a String array, then concatenate all the items to TextBox2 with a space separating them
    Dim lb2Items(ListBox2.Items.Count - 1) As String
    ListBox2.Items.CopyTo(lb2Items, 0)
    TextBox2.Text = String.Join(" ", lb2Items)
    Finally, your last 2 lines use LINQ but I'm not at a computer to debug what they're trying to do.

    With that bit of information, you really need to fix the issues that I address.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width