Solved

I need someone to explain to me exactly how this code works. Please help

Posted on 2008-11-02
1
196 Views
Last Modified: 2013-11-26
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

Open in new window

0
Comment
Question by:ajh305
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

Accepted Solution

by:
Mysidia earned 500 total points
ID: 22862739
It seems self-explanatory.  

The code declares and sets up  'dict'  a hash indexed by the digits 0 through 9,
with a stored value of the letters  associated with that digit
i.e.  "2" to "ABC"
...

Next a variable 'phoneNo'  is assigned to the current value of a textbox at the start of the procedure.

A temporary list "words" starts in the empty state.

A for loop examines each character.
  Within one iteration of the for loop
   the digit is examined

   'listTemp'  is assigned to the list of characters from "dict"
    i.e.  if    the current digit is "2"  then  listTemp  is the list with 3 items,
    A, B, C,
    if current digit is "3"  then  listTemp  is D, E, F,  and so on.

    A temporary list  "wordsTemp"   is reset so it contains no items.

    If there are any items in the "words"  temporary list, they are added to wordsTemp
    and  "words" is again reset to an empty state.

    For every word in the  "wordsTemp"  list
       For every item in the "listTemp"  list of characters
          the word in wordsTemp   PLUS the character in listTemp
          is added to the "words"  list
         

    The iteration of  the outer 'for' loop completes and the next digit is now being examined.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question