In this video, we're going to be talking about array's and how we might use them in visual basic.
And so this begs the question, what is an array?
An array is a specific type of variable in VBA that can store multiple values instead of a single value.
So whereas a normal variable in VBA can only store one value at a time, your array type variable can
store an entire list of values so long as they are the same data type.
So for example, if we had a list of student names John James, Hannah, Connor and Christina, we would
have to create five separate variables if we wanted to store each of those values in its own normal
VBA variable.
If we created an array variable, on the other hand, we could store all five of those names at once
and only require one variable declaration.
So this kind of begs the question, how do arrays work?
So arrays are going to first take your list of values that you're giving.
Turn them into a little less like it's shown on the right and then turn each of those items into an
individual item itself.
So instead of having five separate variables, storing five separate values, we can set up an array
ahead of time to expect five values.
It will create five separate little item boxes.
And then as we assign those item boxes values, each of those items gets assigned its own number.
So in this case, that array on the bottom has green boxes for John, James, Hannah, Connor and Christina.
All five of those names get their own box, their own value, but they all exist within the same array.
They're separated by a numbering system, an indexing system that starts with zero.
And I might not seem intuitive at first, but starting with zero for indexing is pretty standard for
all programming languages.
So instead of counting from one and going up to five four lists of names, the system will start from
zero and go up to four.
And we'll see examples of this when we actually talk about using arrays in visual basic.
You can see here that we have a single array.
It's a single variable, but we're storing all five items separately within our array.
And that's why we like to use arrays, arrays can significantly simplify the code.
Because you can reduce the number of variables that you have to set up a number of variables that you
have to actually go in and store values to and retrieve.
Values from arrays are also a lot more flexible than a regular variable type.
They can continue to grow if we have a longer list.
We can continue to add items onto that list.
They can shrink if we need to shrink them.
Later on.
We realized we didn't have as many values as we thought.
So in this table on the left, this green table, we can see if we were to use standard variable naming
system, we'd have to create five variables.
Student one, student to student three, student four and student five.
We do then have to assign each one of those variables of value.
And later in our code, we'd have to remember which variable meant which name and know which variable
to be referencing at whatever time we need in our code.
If we did this with an array, however, we could create one array variable.
In this case, it would be students and then each of those items in the array could get assigned a value.
So what's one variable declaration compared to the five variable declarations that we would need on
the left?
And this leads into the difference between static and dynamic arrays.
So static arrays have a preset size.
That means they can only take a certain number of items.
And we'll tell the system ahead of time.
Hey, this array that we're about to build for you, it's only going to have four items in it.
All you only ever need to expect four items.
So static arrays are good for when, you know, ahead of time exactly how many items you're going to
have in that array.
But if you had a list and you weren't sure how long your list was and you weren't sure if your list
was actually going to grow or if it might shrink later on, you could use a dynamic array.
And the dynamic Rakan is exactly as it sounds.
Grow and shrink as you add things or take things out.
So in the previous examples, we had exactly five names.
We knew how many names there were going to be.
So we could declare our array as a static array.
Meaning when we declared the array, we tell the system ahead of time recreating an array variable called
students.
And it's going to have zero to four, meaning zero one, two, three and four, all as items within
this array.
And it's going to be a string type variable.
That would be a static array.
This system knows ahead of time.
All right.
I should have index numbers from zero to four.
And that's the only size that this array should ever be.
But if we were worried, for example, that we might have more students in the future or that some of
our existing students, by graduate, we could set up that array to be dynamic, meaning can grow and
shrink depending on however many values we feed that array.
And in this case, when we create the array, we just don't give it a size.
We let the system determine the size as we go.
So in this video, we talked about VBA arrays.
We talked about how they work, why we would use them.
The difference between static and dynamic arrays.
And now we're ready to go ahead and take a look at how to use arrays in visual basic.