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:
- Your Dictionary doesn't have any KeyValue pairs to check if there is an item that contains the key
- Your key that you're checking if it is contained in the Dictionary is a blank String, it was never assigned a value
- 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.