Visual Programming Lecture Five (2024-2025)

Document Details

MasterfulMoldavite3638

Uploaded by MasterfulMoldavite3638

University of Al-Maarif

2024

Ali Tariq Radeef & Safaa Jumaah Wajji

Tags

Visual Programming Data Structures Arrays Visual Basic .NET

Summary

Lecture five on the topic of Visual Programming from the University of Al Maarif in the year 2024-2025, covering data structures and arrays. The lecture includes examples of using arrays in Visual Basic .NET using Visual Studio, with code snippets. The lecture explores defining and using arrays, including one-dimensional and two-dimensional arrays.

Full Transcript

‫جامعة المعارف‬ ‫كلية العلوم‬ ‫قسم علوم الحاسوب‬ Visual Programming Ali Tariq Radeef & Safaa Jumaah Wajji Lecture Five Year: 2024-2025...

‫جامعة المعارف‬ ‫كلية العلوم‬ ‫قسم علوم الحاسوب‬ Visual Programming Ali Tariq Radeef & Safaa Jumaah Wajji Lecture Five Year: 2024-2025 1 Working with Data Structures/ Arrays A fairly common requirement in writing software is the need to hold lists of similar or related data. You can provide this functionality by using an array. Arrays are just lists of data that have a single data type. For example, you might want to store a list of your friends’ ages in an integer array or their names in a string array. 2 Defining and Using Arrays When you define an array, you’re actually creating a variable that has one or more dimensions. For example, if you define a variable as a string, you can hold only a single string value in it: Dim strName As String However, with an array you create a kind of multiplier effect with a variable, so you can hold more than one value in a single variable. An array is defined by entering the size of the array after the variable name. For example, if you wanted to define a string array with 10 elements, you’d do this: Dim strName(9) As String 3 Defining and Using a Simple Array 1. In Visual Studio 2019, click File ➪ New Project. In the New Project dialog, create a new Windows Forms Application called Array Demo. 2. When the Designer for Form1 appears, add a ListBox control to the form. Using the Properties window set its Name property to lstFriends and its IntegralHeight property to False. 3. Add a Button control to the form. Set its Name property to btnArrayElement and set its Text property to Array Element. Arrange your controls so that your form looks similar to Figure 1 because you’ll be adding more Button controls to this project later. 4 Figure 1 5 4. Double‐click the button and add the following bolded code to its Click event handler. You’ll receive an error message that the ClearList procedure is not defined. You can ignore this error because you’ll be adding that procedure in the next step: Private Sub btnArrayElement_Click(sender As Object, e As EventArgs) Handles btnArrayElement.Click ClearList() 'Declare an array Dim strFriends(4) As String 'Populate the array strFriends(0) = “Omer" strFriends(1) = “Ali“ strFriends(2) = “Yasser" strFriends(3) = “Noor" strFriends(4) = “Jamal" 'Add the first array item to the list lstFriends.Items.Add(strFriends(0)) End Sub 6 5. Now create the following procedure: Private Sub ClearList() 'Clear the list lstFriends.Items.Clear() End Sub 6. Save your project by clicking the Save All button on the toolbar and then run it. When the form appears, click the Array Element button. The ListBox on your form will be populated with the name Wendy. 7 Using For Each...Next with an Array 1. Close your program if it is still running and open the Code Editor for Form1. Add the following bolded variable declaration at the top of your form class: Public Class Form1 'Declare a form level array Private strFriends(4) As String 2. In the Class Name combo box at the top left of your Code Editor, select (Form1 Events). In the Method Name combo box at the top right of your Code Editor, select the Load event. This causes 8 the Form1_Load event handler to be inserted into your code. Add the following bolded code to this procedure: Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 'Populate the array strFriends(0) = “Omer" strFriends(1) = “Ali“ strFriends(2) = “Yasser" strFriends(3) = “Noor" strFriends(4) = “Jamal" End Sub 3. Switch to the Forms Designer and add another Button control. Set its Name property to btnEnumerateArray and its Text property to Enumerate Array. 9 Double‐click this new button and add the following bolded code to its Click event handler: Private Sub btnEnumerateArray_Click(sender As Object, e As EventArgs) Handles btnEnumerateArray.Click 'Clear the list ClearList() 'Enumerate the array For Each strName As String In strFriends 'Add the array item to the list lstFriends.Items.Add(strName) Next End Sub 5. Run the project and click the button. You’ll see results like those in Figure 2. 10 Figure 2 11 Passing Arrays as Parameters 1. Return to the Forms Designer in the Array Demo project and add another Button control. Set its Name property to btnArraysAsParameters and its Text property to Arrays as Parameters. 2. Double‐click the button and add the following bolded code to its Click event handler. You’ll receive an error message that the AddItemsToList procedure is not defined. You can ignore this error because you’ll be adding that procedure in the next step: 12 Private Sub btnArraysAsParameters_Click(sender As Object, e As EventArgs) Handles btnArraysAsParameters.Click 'Clear the list ClearList() 'List your friends AddItemsToList(strFriends) End Sub 3. Add the AddItemsToList procedure as follows: Private Sub AddItemsToList(ByVal arrayList As String()) 'Enumerate the array For Each strName As String In arrayList 'Add the array item to the list lstFriends.Items.Add(strName) Next End Sub 4. Run the project and click the button. You’ll see the same results that were shown in Figure 2. 13 Adding More Friends 1.Return to the Forms Designer of the Array Demo project. Add another Button control and set its Name property to btnMoreArrayParameters and its Text property to More Array Parameters. 2. Double‐click the button and to its Click event handler add: Private Sub btnMoreArrayParameters_Click(sender As Object, e As EventArgs) Handles btnMoreArrayParameters.Click ClearList() Dim strMoreFriends(1) As String strMoreFriends(0) = " Ahmed " strMoreFriends(1) = " Mohamed " AddItemsToList(strFriends) AddItemsToList(strMoreFriends) End Sub 14 3. Run the project and click the button. You will see the form shown in Figure 3. Figure 3 15 Array Two Dimensional 16 Private Sub btnTwoDimensionalarray_Click(sender As Object, e As EventArgs) Handles btnTwoDimensionalarray.Click Dim strtwoDarray(2, 2) As String DataGridView1.Rows.Clear() strtwoDarray(0, 0) = "Ali" strtwoDarray(1, 0) = "Omer" strtwoDarray(2, 0) = "Mohamed" strtwoDarray(0, 1) = "Noor" strtwoDarray(1, 1) = "Yasser" strtwoDarray(2, 1) = "Waleed" strtwoDarray(0, 2) = "Abd" strtwoDarray(1, 2) = "Omer" strtwoDarray(2, 2) = "Ali“ For i = 0 To 2 DataGridView1.Rows.Add(strtwoDarray(0, i), strtwoDarray(1, i), strtwoDarray(2, i)) Next End Sub 17