Download presentation
Presentation is loading. Please wait.
Published byΛυδία Αλεξανδρίδης Modified over 5 years ago
1
To understand what arrays are and how to use them
2
Variables Dim MyNumber As Integer MyNumber = 5 Dim MyText As String
MyText = “A String is really just text” Dim MyNumber As Integer = 5
3
Good practice Comments! Meaningful variable names
All variables declared at the start of the subroutine Rename controls (textboxes, command buttons etc to have meaningful name) Use some kind of system for consistency, perhaps all text boxes should begin txt For example txtMaxAmount
4
Arrays An array is a variable that can hold more than one piece of information at a time. Dim MyNumbers(4) As Integer MyNumbers(0) = 1 MyNumbers(1) = 2 MyNumbers(2) = 3 MyNumbers(3) = 4 MyNumbers(4) = 5
5
An example Index Value Start a new VB project.
Add a Button to your Form. Set the Text property of the Button to “Integer Array” Attach the following code to your button Dim MyNumbers(4) As Integer MyNumbers(0) = 10 MyNumbers(1) = 20 MyNumbers(2) = 30 MyNumbers(3) = 40 MyNumbers(4) = 50 MsgBox("First Number is: " & MyNumbers(0) ) MsgBox("Second Number is: " & MyNumbers(1) ) MsgBox("Third Number is: " & MyNumbers(2) ) MsgBox("Fourth Number is: " & MyNumbers(3) ) MsgBox("Fifth Number is: " & MyNumbers(4) ) Index Value
6
A second example You need a list box for this example
Dim MyNumbers(4) As Integer Dim count As Integer MyNumbers(0) = 10 MyNumbers(1) = 20 MyNumbers(2) = 30 MyNumbers(3) = 40 MyNumbers(4) = 50 For count = 0 To 4 ListBox1.Items.Add( MyNumbers(count) ) Next
7
3rd example Still needs a list box!
Dim MyText(4) As String Dim count As Integer MyText(0) = "This" MyText(1) = "is" MyText(2) = "a" MyText(3) = "String" MyText(4) = "Array" For count = 0 To 4 ListBox1.Items.Add( MyText (count) ) Next
8
Work! Write a shopping list program
The program should use an a array of strings to store 5 items in a shopping list A for loop should be used to get the data into the array An input box should be displayed for each item and the user will be expected to type the item needed into the input box Once the user has inputted 5 items they should be displayed in a listbox. The output list should be numbered and displayed using a for loop
9
Shopping List Extension
Add a sort button that will display list in alphabetical order Find out about multidimensional arrays and add an additional “column” to the array to store quality wanted User needs to be able to input this quantity and it should also be displayed Add ability to save/load the list Add a save / load dialog box to allow user to choose file Ability to print the shopping list Ability to the shopping list
10
Arrays where Boundaries are not known
You can declare an array where you don’t know how many values you want to store: Dim shoppinglist() As String Could then ask user for how many items to go onto shopping list and then make the array big enough ReDim shoppinglist(number_of_items) Do we have to do anything with the number_of_items before ReDimming the array? You can use the number_of_items variable in a for loop to read in and output the list… Update your shopping list program to find out how many items the user wishes to store.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.