Presentation is loading. Please wait.

Presentation is loading. Please wait.

VB.Net Programming Console Application

Similar presentations


Presentation on theme: "VB.Net Programming Console Application"— Presentation transcript:

1 VB.Net Programming Console Application
Walkthrough Example : 2 Dimensional Array PhoneList

2 Narrative Memory Code Screen 1 2 1 2 3 4 5 PhoneList Fail
This statement declares the Array in memory. The Array is called PhoneList It has 5 rows and 2 columns.* * There is also a row 0 and a column 0 –but we shall ignore those in this example. PhoneList 1 2 1 2 Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

3 Narrative Memory Code Screen 1 2 1 2 3 4 5 PhoneList Fail
This line is a comment. It simply states what the next section of code will perform. PhoneList 1 2 1 2 Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

4 Narrative Memory Code Screen 1 2 1 Jim 2 3 4 5 PhoneList Fail
“Jim” is placed into the array PhoneList. It is placed at row 1 column 1. PhoneList 1 2 1 Jim 2 Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

5 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 3 4 5 PhoneList Fail
“ ” is placed into the array PhoneList. It is placed at row 1 column 2. PhoneList 1 2 1 Jim 2 Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

6 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 3 4 5 PhoneList
“Ann” is placed into the array PhoneList. It is placed at row 2 column 1. PhoneList 1 2 1 Jim 2 Ann Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

7 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 257 5073 3 4 5
“ ” is placed into the array PhoneList. It is placed at row 2 column 2. PhoneList 1 2 1 Jim 2 Ann Code 3 Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 5 Fail Screen

8 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 257 5073 3 Raf
These statements continue to fill the PhoneList array with Names & Phone Numbers. Note The first number in the brackets states the row The second number states the column. PhoneList 1 2 1 Jim 2 Ann 3 Raf Code Sub Main() Dim PhoneList(5, 2) As String 'Fill the Array with data PhoneList(1, 1) = "Jim" PhoneList(1, 2) = " " PhoneList(2, 1) = "Ann" PhoneList(2, 2) = " " PhoneList(3, 1) = “Raf" PhoneList(3, 2) = " " PhoneList(4, 1) = "Yen" PhoneList(4, 2) = " " PhoneList(5, 1) = "Will" PhoneList(5, 2) = " " 4 Yen 5 Will Fail Screen

9 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 257 5073 3 Raf
This next example will search for a name within the PhoneList & display the phone number. However, if the name is not found in the PhoneList a message “Name not in list” is displayed. PhoneList 1 2 1 Jim 2 Ann 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen 5 Will Fail Screen

10 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 257 5073 3 Raf
This line is a comment and simply states what the next section of code will do. PhoneList 1 2 1 Jim 2 Ann 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen 5 Will Fail Screen

11 Narrative Memory Code Screen 1 2 1 Jim 355 5671 2 Ann 257 5073 3 Raf
These DIM statements set up memory to store: SearchName -the name to search the list for. Found – boolean (True/False value) initially set to FALSE but set to TRUE if a match is found for the SearchName. RowCount – to count through the rows in the array. PhoneList 1 2 SearchName 1 Jim 2 Ann Found 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will Fail Screen

12 Narrative Memory Ann Code Screen Enter Name…: Ann 1 2 1 Jim 355 5671 2
The console screen is cleared. The user is prompted to enter the name to search for. The user types the name ( Ann in this example) and it is stored in memory as SearchName. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will Fail Screen Enter Name…: Ann

13 Narrative Memory Ann Code False Screen Enter Name…: Ann 1 2 1 Jim
Found is set to False – note it is set to true if a match for SearchName is found in the PhoneList. RowCount is set to 0 – This is used to count through each row of the PhoneList. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will Fail Screen Enter Name…: Ann

14 Narrative Memory Ann Code False Screen Enter Name…: Ann 1 2 1 Jim
This while loop will continue while Found = False (I.e. we have not found what we are looking for) AND RowCount is less than 5 (I.e. we have not reached the end of the PhoneList. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will Fail Screen Enter Name…: Ann

15 Narrative Memory Ann Code False 1 Screen Enter Name…: Ann 1 2 1 Jim
This statement increments the RowCount so that we move through each row of the PhoneList ( that’s one row each time we go through this loop) RowCount is incremented fro 0 to 1 PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 1 Fail Screen Enter Name…: Ann

16 Narrative Memory Ann Code False 1 Screen Enter Name…: Ann 1 2 1 Jim
This IF statement compares the SearchName with column one of the PhoneList where row is RowCount. That is row 1 column 1 = Jim. If they match then Found is set to True If they do not match Found remains False. Ann does not match Jim PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 1 Fail Screen Enter Name…: Ann

17 Narrative Memory Ann Code False 1 Screen Enter Name…: Ann 1 2 1 Jim
The variables are tested again to see if the while loop should run through again. Found is still = False AND RowCount is still less than 5 So the loop runs through once more. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 1 Fail Screen Enter Name…: Ann

18 Narrative Memory Ann Code False 2 Screen Enter Name…: Ann 1 2 1 Jim
This statement increments the RowCount so that we move through each row of the PhoneList ( that’s one row each time we go through this loop) RowCount is incremented from 1 to 2 PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found False 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 2 Fail Screen Enter Name…: Ann

19 Narrative Memory Ann Code True 2 Screen Enter Name…: Ann 1 2 1 Jim
This IF statement compares the SearchName with column one of the PhoneList where row is RowCount. Row 2 column 1 is Ann This time SearchName does match Ann. So Found is set to True. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found True 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 2 Fail Screen Enter Name…: Ann

20 Narrative Memory Ann Code True 2 Screen Enter Name…: Ann 1 2 1 Jim
The variables are tested again to see if the while loop should run through again. Found is no longer = False and so this loop will end. Note that RowCount is 2 and that row 2 column 2 holds the corresponding phone number. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found True 3 Raf Code 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 2 Fail Screen Enter Name…: Ann

21 Narrative Memory Ann Code True 2 Screen Enter Name…: Ann
The IF statement checks to see if Found is equal True. It is. So the Phone number at row (RowCount i.e.2) column (2) is written to the screen. PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found Code True 3 Raf 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 2 Fail Screen Enter Name…: Ann Number……..:

22 Narrative Memory Fred Code False 5 Screen Enter Name…: Fred
If no match for SearchName had been found in the while loop -Found would have stayed set to False. The RowCount would have reached 5 and terminated the loop. Then the message in the Else part if this IF statement would have written Name not in list on the screen. PhoneList 1 2 SearchName Fred 1 Jim 2 Ann Found Code False 3 Raf 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 5 Fail Screen Enter Name…: Fred Name not in list

23 Narrative Memory Ann Code True 2 Screen Enter Name…: Ann
This statement waits for a key to be pressed on the keyboard. Once the key is pressed this example program ends PhoneList 1 2 SearchName Ann 1 Jim 2 Ann Found Code True 3 Raf 'Searching the array Dim SearchName As String Dim Found As Boolean Dim RowCount As Integer Console.Clear() Console.Write("Enter Name.....:") SearchName = Console.ReadLine( ) Found = False RowCount = 0 While Found = False And RowCount < 5 RowCount = RowCount + 1 If PhoneList(RowCount, 1) = SearchName Then Found = True End If End While If Found = True Then Console.WriteLine("Phone Number...:{0}", _ PhoneList(RowCount, 2)) Else Console.WriteLine("Name not in list") Console.ReadKey() End Sub 4 Yen RowCount 5 Will 2 Fail Screen Enter Name…: Ann Number……..:

24 End of 2D Array example


Download ppt "VB.Net Programming Console Application"

Similar presentations


Ads by Google