VB.NET 2013 - Arrays

Sharing buttons:

Bodey nichols Tingler here again for

another 2013 tutorial video and

today we're going to cover arrays and

the multitude of uses and a lot of

advanced features hopefully before the

end of this video i'm going to warn you

right now this is going to be a long one

okay and let's just start with a bit of

a slideshow and give you a bit of

background and then we'll get into the

guts of it with some vb and then we'll

do some more examples as we get along so

basically let's before you begin have a

look at something very simple and

something that we covered in the second

video of all times and that is a

variable alright

basically what you should know by now is

that it's a place to store one piece of

data it could be of different types okay

that's not just what it can be there's

more than that obviously okay and it has

a name that we can easily identify so if

we were going to visualize this it would

be a box where we can store stuff it

would have a name such as num and then

we put a number inside of it and that

essentially there this whole box here

would be a variable just a single

variable but let's just say we want to

store five numbers in total well

realistically the simple answer for that

is just make five boxes with five

different numbers to put inside of it

that's good look at the same name or

very similar names I should say okay and

they got the exact same different types

of values however tedious and the

biggest problem is the time to access

that data because this stuff is

scattered around memory okay they don't

sit neatly next to each other which

means is the time to access each piece

of data is actually quite long so where

do we go from me well basically we look

at it array so essentially set of values

which have some kind of relation okay

and that could be something as simple as

a set of numbers

okay a set of random numbers it could

even be something like students marks or

names of months and things like that so

let's have a look a quick one they have

the same name the homogeneous which

means homogeneous I mean means all the

values are the same type they don't have

to be the same values they're the same

type and this is what an array would

look like okay in contrast to what the

variables look like

for they sit sequentially together they

all have the same name but they have

different values and each one of these

boxes we call an element okay so

basically every single box here is

considered numbs but they all have

different values well that's good

so let's have a look at a couple more

examples because we can't we don't just

have to have integers for our data type

what if we want singles let's say we

have to calculate the rainfalls over a

certain month and we capture say eight

of them okay what if we want the names

of the month we have twelve of them so

we make them strings what about if we

need to know if we paid our bills over

the year was four quarters to a year and

there's boolean so true true false and

true you can use any type of data in an

array so long as every single element in

your array is the same data type and

that's fine

so basically well we have all these

different elements how do we access each

individual one well we use a thing

called an index sometimes known as the

subscript okay and that's pretty much

what I explained in that sentence

generally there's all right let's pull

it from the other slide don't generally

start at zero most cases they will and

then they'll go to the highest index so

if we have a look down here January will

start at 0 and it'll go all the way up

to 11 so if we have 12 boxes or 12

elements then it will start 0 and go all

the way to 11 okay so how do we access

and how do we use these indexes or

indices to access each element well just

do it like this months that's the name

of our array 1 which is the index equals

our value and that's pretty much it I

tell you what access h1 all right so how

do you declare this array okay we'll go

through some examples in a second but

basically if we want to clear our

integers which we had a look at earlier

DIMMs num for as integer Y for well what

we actually do the number you put in

that in those brackets is the highest

index so if we want 5 numbers you put 4

and that means will range from 0

no all the way up to four which will

give us five elements okay same thing

for temperatures okay that'll make ten

of them

twelve months and four bills paid okay

and that's how you would go about

declaring it so basically that's all the

theory out of the way so let's have a

look at some simple examples using

Visual Basic so let's just say I'm just

going to make a quick array with three

numbers and let's get them printed onto

the screen so Jim numbs and this time

open a bracket and you specify the

highest index so if I want three numbers

I put two and then you go as your data

type and I'm just going to go integer

all right and then let's fill it with

some data okay thinking that the first

element has the index of zero what you

do is you go nums

zero equals something so we can go ten

or something similar to that the one

thing you can't do we'll put slashes

I've been programming and other stuff

too long you can't do this that doesn't

make sense because what numbs is is the

set of three numbers and what I'm saying

here is I want my set of three numbers

to equal one number which realistically

does not make sense so let me just get

rid of that and that's just a quick

explanation to why we can't do that

numbs one equals let's say 100 numbs

two equals a thousand why not okay and

let's say I want to print all these

things to the screen okay it could be

easy enough for me just to do a right

line and we do the same thing again if

you want to print the numbers to the

screen you can't just do nuns and I'll

quickly demonstrate what it does just as

an example okay it will work you won't

get any errors but this is what you get

system dot int 32 bracket bracket so

what this is actually saying is nuns is

an array which is the squares of

integers that's what we want we want ten

hundred one thousand so we could do this

break the first number

brilliant the second number Bruin the

third number and then give that a go

and there's our three numbers ten

100,000 that's great now what if we want

what if we had a thousand numbers

printing all those thousand numbers to

the screen would be extremely tedious so

let's introduce to you a nice little

loop that I have not explained before in

the past this is called a 4h and what it

does is to every element that's in our

array it will do our code or execute our

code so you go 4-h let's just call it

element as an integer in nones

let's write the element to the screen

okay let me explain that in a little bit

of detail by showing you the code so

there's ten hundred and a thousand with

these three lines of code so for every

element in our array write it to the

screen so the for each loop can be

really really powerful when you get used

to it and it's just a quick introduction

to how it works but for the moment I

would really love to set up the array

that we had for our months so let's have

a look at this one here we're going to

make an array of them and we're going to

get them printed to the screen as a

little menu sort of thing so I'm going

to set that up now if you want to I'm

going to stop the video and come back

when I finish this thank you everybody

welcome back I've got my array set up of

my 12 months and I filled it up with all

the data and well why not let's just get

a nice little neat menu printed the

screen and I'm going to use just a

traditional foliage I could use a for

each okay exactly the same way we used

it before and I'm going to suggest you

have a go with that but I'm just going

to show you a quick little for loop

which will go through each one of my

elements in my array notice how in my

for loop I've got for I equals or for I

as integer equals 0 to 11 notice how I'm

using the small index and the largest

index in my loop okay that's probably

the most important piece to take away so

what I do is I'm just going to put a bit

of spacing put a number there okay what

number am I going to put there well I'm

going to put I

plus one bear with me for that one okay

whoops I forgot never sinned okay and

then I'm going to close that bracket and

put my month on the other side all right

that's going to look pretty complicated

for the moment but bear with me let's

have a look at what it does not bad

pretty neat okay let me simplify this a

little bit for you let me take out this

plus one all right run the program again

notice how it ranges from zero to 11

because those are air indexes however if

I put the plus one there just in that

little spot it just seems a little bit

nicer when you range from 1 to 12

especially when the user just the

regular users not going to know that an

index starts zero okay so that's what

I've done so basically I print the start

of the bracket the number inside the

bracket the other side of the bracket

and the months itself so if you ever

wanted to come up like social calendar

for whatever reason and you want

something to select a month this is such

a quick way to print the months to the

screen and if you put this piece of code

just these three lines inside of a sub

and you can call them at any time you

want let's say if it's a calendar

program you're going to be printing the

months to the screen a lot of times so

those three lines of code are going to

save you a hell of a lot of time and a

hell of a lot of heartache okay it's

really easy to make a change if you've

got an error in your code for whatever

reason so look right now all we seem to

be doing is setting up arrays with

values inside of them and putting them

on the screen this is going back to the

old days where we were just using right

line and red line that's pretty boring

out so what we're going to do is we're

going to let the user start to interact

with this and I think the nicest one to

start with is let the user type in ten

numbers and then we sort the numbers for

them and we spit them back out okay so

what I'm going to do is just quickly

wipe this code and set up our array so

if I want ten numbers and I'm just going

to call them numbs just like before then

I want to put nine in the brackets

because it goes from zero to not

and this time I'm not going to start

filling up the array with my own numbers

I'm going to let the user choose

whatever they want so first of all I

always like to tell them what's going on

please enter a number okay and we'll

make that a bit more obvious soon excuse

me then I'm going to put none zero

equals console.readline so we can

actually use none zero numbers one

number two just like any other variable

that we've ever used with redline okay

now realistically I've got ten numbers I

would have to do these lines of code

nine more times but being the lazy

person I am I'm just going to use a

quick for loop starting at zero ending

at nine and I'm going to take this code

jam it in there and the only thing that

I have to change really is this number

here because if I don't change that

number every single time we type in the

number it's going to repeat 10 times

every time we type in the number those

10 times it's just going to change the

first number only which isn't what we

want we want to change the first number

and the second number to the third

number and so forth

so really we don't want it to be zero

and we don't want to be one we don't

want to be - we don't want to be any

number we want it to be a variable I

okay so first of all I reckon we should

probably test this out to make sure it

works now I'm going to check in what's

called a breakpoint down here on this

red line now I haven't explained

breakpoint before so I'm going to press

f9 on my keyboard and you can even

right-click and generally go breakpoint

and add it I use f9 on the keyboard to

add mine in what it does is it pauses

the program and I'm going to have a good

look at what nones is doing so let's

press play and enter our ten numbers

and it breaks awesome let's jump down

here for a second he you can see is our

array its length equals 10 because we

have 10 elements in this IRA if I expand

this you can see each individual number

that I've just typed in which is awesome

so what I said before is we're going to

sort these numbers and then print them

back on the screen so the user can see

their numbers in the set order right and

it's a very very quick command to sort

an array that's just array dot sort and

in brackets you put the name of your

array so our array is called nones

so you pass it numbs okay excuse me I'll

put my breakpoint in there just to make

sure it works first 51 4 92 but not the

10 numbers let's have a look

there's my Nuns let's just expand this

for a second and notice that array dot

sort has put all of these in a nice neat

little order for us isn't that lovely

yep close you you're done

and let's just present it back on the

screen so how do we print it back to the

screen but I'm going to use the for each

loop this time why because I can because

I want to for each element as an integer

in Nuns

right line the element alright what I

think is probably a good idea to before

your loop not inside the loop but before

you loop just tell them what you're

about to do let's still number sorted

let's have a go one more time and then

we'll move on

their numbers sorted and they're in the

perfect daughter okay from the negative

down to 945 so that there is just some

quick examples of how you can use arrays

and what you can do with them

however if you watch the previous video

we were talking about extending our

student data base so we can accept more

than one student in the program so so

far I'm going to leave this code and

save it but I'm going to open up my

previous project okay back to classes we

were just talking about we're just

finishing the video we have a student

setup we have a first name and last name

on a date of birth for every single

student which is great however we only

have one student working in our program

and I was just starting to explain how

you could add a second a third a fourth

and a fifth and so forth however really

what you really really really want to do

is you want to set up an array of

student and that way you can have as

many as you want okay let's say for

instance I'm going to get rid of this

and we're going to make this program

accept up to ten students okay only just

adding them and viewing them nothing

special just adding and viewing those

students so basically first of all I'm

going to get rid of this line and we're

going to do it again so let's dim

students I like to use plurals just to

explain that is an irate and I want ten

of them so I put nine as students now

here's the first problem

before we run into a problem that

classes don't get allocated memory when

your program starts well thanks windows

sorry everybody and so what we did is we

use the new keyword to tell Visual Basic

that we want to allocate some memory to

that class however we get an error and

we're not allowed to use a new keyword

with an array array s cannot be declared

with new okay which sucks so it means

that for each student we have to go

through it and set it to a new student

and you may have already picked up what

I'm about to do because I'll use that

for each student as look I'm just going

to call it element as student in

students element equals new student

well not create allocate memory all

right and we are ready to roll however

before we begin our program we need to

be able to keep track of how many

students are in our program at any time

so what I'm going to do is dim a student

count as an integer and started at zero

because we start with zero students in

our program okay and this is going to be

a very very important number because if

you think about it if we start the

number zero and we go to add a student

what position or what element are we

going to put that student inside of well

we want to put it in the first element

which is the index zero so we use

student count for that one once I've

added that first student we add one to

student count so it'll increase to one

and then we go to add another student

well where we want to place the second

student in index number one okay and so

student count is going to keep track not

only of how many students we have in our

program but where we put the next

student into our array now this is not

going to have any Hara handling it's

probably gonna be very buggy we're just

going to get it working to start with so

let's go down to student add which is

this one and you can see we've already

broken it because we deleted student 1

however as I explained before we just

made our Ray called students and we need

to incorporate student count for our

index so instead of student 1 it's going

to be students in the bracket instead of

putting 0 because that means every time

they type in a student it'll be the

first student you use student count for

this instance okay and this as we

plastered all the way down there and

there okay

so that there seems to be pretty damn

fixed if you ask me however the problem

is view student we're going to run into

a few little problems however what I

want to do is just comment out these

ones just going to get rhythm okay it's

not going to do a single thing but um

we're going to come back and fix that

yeah so what we need to do if you just

think about ad student we're adding in

the first name and if you look at the

syntax we've got the array then we've

got the index and then we've got the

member of our class and this is

sometimes with a lot of people get

confused because we're going to Ray and

index and then the member okay the more

you use is the more it's going to become

really really obvious and basically by

the time we get down to date of birth we

have entered in our first student so

what we need to do is increase student

count by one okay another word for that

is just increment okay increment needs

add one so we're just going to move to

the next value and when we go to add the

next student student counts should be

one and then we go to add the next

student student count should be two and

so forth

and that way it's keeping track of the

next position for us okay so what I'm

going to quickly do is I'm going to put

a little message just here and we empty

right line and I'm going to put number

of students and then just put student

count after that so what it's going to

do is basically say welcome can we have

any students I have and then give us the

option to add alright let's try this

program and let's make sure it works

number of students let's go to add John

Wow broken that didn't work now if you

might be able to say my for each didn't

work so what I'm going to do is I'm

going to have to go back I'm gonna have

to fix up my 4-h I'm gonna have to

change it into a for loop just a

traditional one and we're going to have

to go students I sorry everybody I was

trying to do something new

and I shouldn't have especially in a

video so John Bob first of s the first

student is entered number of students

one what about Jill

Bob why not first a second of the first

student entered and you can see the

number of students is growing in our

program which is perfect now let's fix

this view a student thing basically a

problem is if we want to view a student

before all we have was one student so

all we needed to do is print first name

last name and date of birth okay that's

all we were doing down here that's quite

easy when you've only got one student

but now that we've got more than one

student we it's not a probably a good

idea to print every single student in

one go so basically what we should be

doing is printing them in a nice neat

little list so this is where we can do a

for each okay trust me on this one so

for each element as a student in student

sir okay I'm going to take these three

lines of code I'm not going to take red

line or clear because I don't want to

repeatedly red line or repeatedly clear

okay but we're not going to do student 1

what are we going to do we're going to

do element because we're doing a for

each element paste paste now

realistically what we should see is all

the students in nice little blocks

well should John Bob my one one entered

let's add one more just for good let's

get chilled in there

Jill Bob first a second first and let's

view them okay now here's the snag that

I was hoping to run into fantastic now

as you can see because we did a 4-h it's

going to go through every single element

in our array and you can see Bob there

or john and jules there there's our

first one there's our second one there's

a third one there's a fourth one there's

a fifth one there's a sixth one all the

way down to our tents element now array

which is all well and good but

if we only want to display the two that

would be important to our program well

once again we're going to have to revert

this back to a four loop what and we

don't want to go from we can't use

element anymore because it doesn't exist

so what are we actually looking at well

we're looking at students I okay you

just have to remember that one if you're

using a for loop and your variable is I

then you make it students I now we're

not going to go from zero to nine

because that's just going to spit out

every single student just like we had


what we want is every student that we

have and how do we know how many

students we have well we have this neat

little baby right here student count so

what we're going to do is use student

count instead of nine but we're going to

have to do minus one on the end because

if we have two students in our program

those two students are going to be

indexed to zero and index one okay and

if you were to read it like this we'd be

going from zero to two which means that

we'd print out three students put a

minus 1 there and we're down to two

students so let's just quickly test it

John Bob one one one Jill Bob one two

one and let's just quickly view those

students there's John there's Jill done

that's perfect and that's pretty much

how you use what's called an array of

classes or an array of objects okay and

this is how you make it really really

powerful is these indexes or indices so

whatever you want to call them sometimes

can be really complicated but other

times they can be really really handy

and believe it or not we're not actually

done with this video let's jump back to

the slideshow and let's have a look at

another concept which is quite

complicated but very very interesting it

is multi-dimensional arrays okay right

now we only have one index which means

we can only go one direction

we only have the width on our index what

if we wanted more than that what if we

wanted height or even depth okay or even

more that to our arrays we can actually

have more than one dimension to our

array okay and sometimes uses for this

would be in games or very complicated

applications doesn't have to be

complicated but it can be so an example

for a game would be if you've got a 2d

top-down game such as pokemon and when

you move up down left and right you

notice that you move in little blocks

okay and in background those little

blocks would be represented by a

two-dimensional array because it has

length and it has height to it okay

let's have a quick example of this one

and I'm going to use my own example I

have a number of students in my classes

right and every single student has a

couple of tasks throughout the year for

example in my software design

preliminary class they have three

assessments they have their first solo

task they have their group task and they

have an interview examination okay so

what I'm getting at is I have all these

numbers okay and across the top I have

the tasks and they range from zero to

two okay that's the index for my tasks

so you can notice that these are the

results for task zero these are the

results for task 1 and same for task two

and going up the side we have the

students ok they range from 0 down to 4

so basically what that means is that all

these marks belong to the first student

or these marks are a second student and

so forth as we go across so basically if

we wanted to pinpoint well how well did

student 3 go in their examination well

student 3 examination would be that one

so they've got 66 out of 100 which is

average okay and that's what you would

use as a 2d array well how do we declare

something like that well this is what

you do you specify the first dimension

just like we used to

then you put a comma and then the next

dimension and you can extend that again

you can do another commerce depth

another comma phone next one and so


okay I'm not entirely sure what the

limits are on that but right now I don't

think I've ever gone past four

dimensions and if you get to five

dimensions I think it programs a little

bit too complicated and you need to go

back to the drawing board okay now how

do I access each individual mark well if

you have a quick look at each one it

essentially breaks down to what's called

an x and a y position okay if you're

good with graphs you'll be good with

this so if I wanted to know how well did

student to go on this second assessment

well second assessment student to got 55

so we always go on the x axis before we

go on the y axis so what I mean by that

well for student 0 I'll show task 0

student 0 they got 10 tasks to student

one they got 71 and so forth in that

particular example just to give you one

more example before we actually do

anything in programming okay I'm going

to quickly open up Excel a lot of people

have used Excel in the past right now

you might notice that Excel seems to

have a lot of cells and if you look at

your x-axis and if you look that's your

y-axis okay and where they meet is the

data value in the middle so the Excel

program is a great example of a two

dimensional array and in fact it's a

good example of a three dimensional

array because not only do we have

columns and rows but we have worksheets

okay so you can consider it X Y Z okay

so that would be a three dimensional