So now that we know a little bit about arrays and specifically static arrays we can take our knowledge
a little bit further by talking about dynamic arrays which will grow or shrink to accommodate however
much data we have at any point in our code.
And the nice thing about dynamic arrays is if we wanted to add this fifth name if we wanted to add Eddie
and we wanted to do it you know halfway through our code once we've already gotten a couple pages worth
of code done instead of having to scroll back through and find where we declared our variable originally
we could just read a mentioned our variable whenever we want.
Meaning if our variable was originally 0 2 4 we could change it halfway through the code to be 0 to
5.
And without having to come back through and change our existing items.
So as an example of this let's create another public sub this time we'll call it dynamic array and this
time we'll call our variable students dynamic so that we can see this is our dynamic array and we'll
tell it it's a string.
Again you can see right off the bat whereas we originally put a sizing in this original declaration
the static declaration up here.
We have not put a sizing in our parentheses for our dynamic array.
So all we told the system this time is in our original declaration here's the name of our array.
We can tell it's an array because we have the parentheses here but we haven't given you a size.
This could be any size that we want it to be.
And it is string which means text and this is different from our static declaration where we said here's
our array name.
Here's the size of the array and it is text.
So this time we can resize this array to be whatever we want it to be and to resize things.
You re dim them you reader mentioned them so we'll go ahead and read amend.
Reader mentioned this as an example and we'll read I mentioned this to hold up to fourth item you don't
technically need to write the add string at the end of this.
And that's because you already told the system that this array is a string.
All that we're doing and read em is resizing it which means the only thing that should change is this
value here.
So we can actually take this off and hit enter.
And now our dynamic array should hold between the zero and the fourth item so we can go ahead and set
that up the zero item will be John
the first item will be James The second item will be Hannah
third item Connor
fourth item Christina and I'll go ahead and set up the fifth item but I'm going to do it with an apostrophe
in front of the line so that it is a comment line and doesn't affect the code at all the code was actually
jump over this line but we'll have it when we need it in a second and we'll say Eddie and then I'm going
to create the same input box this time I'll use X as my variable
we'll ask the same question Which student number do you want to view.
And we'll title it student I.D. again to this should interact with the user in the exact same way that
our other coded and we'll create the same output box at the end.
This little message box that says you selected and our students dynamic array variable X and we'll call
the title of the Spock student name just like we did with the other one so these two pieces of code
are the exact same for our message box for our input box.
The only thing that's changed is this is a dynamic array because we haven't originally sized it.
Whereas this is a static array because we did give it an original size.
So just to get an example of how this runs make sure that this is working OK we'll go ahead and run
this code.
So we'll go to macros select dynamic array this time and click Run says which student number do you
want to view.
We'll say we want to view number one and we see it selects James so that we know this is working correctly.
Now if we go back into our code and we take out this apostrophe and we say OK I'm going to give it a
fifth item.
Now we know that this probably won't work because we size this to be four items long but we'll try to
run it anyways.
We'll go macros dynamic array run and we see it is going to give us an error and we can click debug
and see there is occurring on the same exact one.
Now the difference here is rather than coming back up to the beginning to our original declaration and
resizing it there we can continue to resize this array.
However many times we need to without changing the original declaration which means if I type read him
students dynamic 0 to 5 here then I will have resize my variable to contain a fifth item.
In this case we're going to say the fifth item is Eddie.
And so when I go back to run this we should see that the fifth item is held correctly so we'll highlight
dynamic array and we'll click Run we see everything seems to be working OK we'll put an item number
five and click ok and we see it has selected Eddie so we know we have correctly re sized our array variable
to contain that fifth item.
Now one thing that you'll notice is if we try to run this and find our second name you see it returns
nothing.
And the reason for this is when you read size your dynamic variable.
Be careful about where your read resizing it and how your resizing it.
So in this case we've re sized our variable after we've already assigned the zero through fourth item
and that means that all of this data that we've previously assigned right now it's being dropped and
we don't necessarily want that data to be dropped and there's a way to prevent that from happening.
And so this is preserve so we'll type the word preserve out in front of our resized variable declaration
here.
So instead of just reading resize my variable I want it now to be this variable to be this long we're
going to say resize our variable but preserve the values that are already in my variable.
And this is how long I want it to be.
So now that I've put preserve here this data should not be dropped when I resize my array instead.
This this data will all be kept in the array and all that we'll be doing is adding a fifth item spot
which we turn around and immediately assign the value of Eddie.
So now when I try to run this
and I again look for my second value we can see it has correctly held all of that data with it whereas
it originally dropped all of that data.
So we have preserved that data as we were resizing our dynamic array.
So this has some some great implications for if you don't know what size array you need to begin with.
Here's an array type that you can continue to add items to as needed.
You can preserve all your data as you're adding items.
So this is a great option for if you're not sure how many or how few items you're going to have in a
list a dynamic array is the way to go.
But this does kind of beg question if we're going to continue adding items to my list can I find out
how many items I have at the end of my list and there are ways to calculate that.
And that is using the upper and lower bounds of the system.
And in this case the upper bound is this five right here it's whatever the item number is that is the
maximum of this array the lower bound is this number here it's whatever the lowest item number is.
That can be within that array.
So if we wanted to for example find out what the upper bound is we could create this I integer variable
like so that we could set ie equal to the U bound which is the upper bound of students dynamic and hit
enter.
We could go ahead and create a little message box right here at the end to just tell us what the upper
bound is.
And if I run this we should see the number five pop up in the secondary message box.
So I'll go ahead and do that as an example.
Macros dynamic array run.
And this doesn't matter we'll just say we want to view the fourth name which should be Christina.
So this part is checking out okay.
Now when we click OK we should see the number five pop up which it does.
So this means our system has correctly counted the upper bound of our array and we could do the same
thing with our lower bound.
So if instead of the upper bound which is you would change this to El bound which is lower bound we
should see zero show up in that system and you can use this at a multiple different ways we could change
this to be you bound minus the El bound students dynamic so if you had a this dimension from 2 to 5
then this would only retrieve you know you have three different items that you can store data in and
this particular array that you've created there's lots of different ways to use the upper and lower
bound of the system but this is an example of how you might use this to count the number of items that
you can put into your array in this video we cover dynamic arrays and how they can help with unknown
sizes of data and the next video we're going to talk about creating multi-dimensional arrays which we
can use to cover larger ranges of data all at once.