ajh305
asked on
I need someone to explain to me exactly how this code works. Please help
I got this code from an expert. The program permutates all the letters of a phone number input. The program works great but i need help understanding it so if someone could help that would be great!
Public Class Lab4
Public Function makeCharList(ByVal chars As String) As List(Of Char)
Dim charsList As New List(Of Char)()
For Each c As Char In chars.Trim().ToCharArray()
charsList.Add(c)
Next
Return charsList
End Function
Private Sub generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generate.Click
'Use of a dictionary may be better, it uses same concept as KeyValuePair but already has it in a collection
'Therefore, you will not have to recreate the wheel
Dim dict As New Dictionary(Of Char, List(Of Char))()
Dim words As New List(Of String)() 'To store word possibilities
Dim wordsTemp As New List(Of String)() 'Temp word lists as needed
Dim listTemp As New List(Of Char)() 'Temp character list
'Adds and assigns each digit to an array of characters
dict.Add("0", makeCharList(""))
dict.Add("1", makeCharList(""))
dict.Add("2", makeCharList("ABC"))
dict.Add("3", makeCharList("DEF"))
dict.Add("4", makeCharList("GHI"))
dict.Add("5", makeCharList("JKL"))
dict.Add("6", makeCharList("MNO"))
dict.Add("7", makeCharList("PQRS"))
dict.Add("8", makeCharList("TUV"))
dict.Add("9", makeCharList("WXYZ"))
Dim phoneNo As String = phonenumber.Text.Trim() 'assigns value of textbox control to local variable
'Goes through each digit of your phone number
For Each digit As Char In phoneNo.ToCharArray()
listTemp = dict(digit)
If listTemp.Count > 0 Then
'''''''''' setup words list ''''''''''
wordsTemp.Clear()
If words.Count < 1 Then
wordsTemp.Add("")
Else
wordsTemp.AddRange(words)
words.Clear()
End If
For Each word As String In wordsTemp
For Each c As Char In listTemp
words.Add(word & c)
Next
Next
End If
Next
MsgBox(String.Format("Writing {0} words to list", words.Count))
lbWords.Items.Clear()
lbWords.Items.AddRange(words.ToArray())
MsgBox("Generate process complete!")
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.