An array is a series of memory locations – or ‘boxes’ – each of which holds a single item of data, but with each box sharing the same name. All data in an array must be of the same data type.
Watch the video, read the website and have a go at the test to ensure you understand what is meant by an array before you start programming in
Step 1 - Setting up and initialising an array
Create a VB project.
Declare an array called MonthName, of 12 string values, to hold the 12 months of the year.
Dim MonthName(12) As String
Initialise the array with the names of the months of the year - Hint - this will need to go under the Form_Load so when the form loads the months are loading into the array (i.e. the area of memory - MonthName you created)
MonthName(1) = “January”
MonthName(2) = “February”
MonthName(3) … etc. …
Inside the computer, in the area of memory MonthName, it will look like this
(pos) MonthName Array
(1) January
(2) February
(3) March
(4) April
(5) May
(6) June
(7) July
(8) August
(9) September
(10) October
(11) November
(12) December
To output the array into a listbox to check months have been stored correctly
For MonthNumber = 1 to 12 (MonthNumber needs to be declared as a variable)
lstMonths.items.add(monthname(MonthNumber)
Next
Step 2 - Write code to retrieve month name
Allow the user to enter a month number. You must display the name of the month.
Dim MonthCountAs Integer
MonthCount = Input.text
Output MonthName(MonthCount)
Step 3 - Task
For each month, store the number of days in that month and display the appropriate number of days along with the month name.
HINT: use a new array to store the number of days
Pseudocode so far…..
Dim MonthName(12) As String
Dim MonthNumber As Integer
MonthName(1) = “January”
MonthName(2) = “February”
MonthName(3) … etc. …
MonthNumber = Input.text
Output MonthName(MonthNumber)
Add code to above:
Dim MonthDays(12) As Integer
MonthDays(1) = 31
MonthDays(2) = 28
MonthDays(3) … etc. …
Output MonthName(MonthNumber) and MonthDays(MonthNumber)
At the end of Step 3, you should have 2 arrays which looks like this …….