1 00:00:01,120 --> 00:00:07,030 So now that we know a little bit about arrays and specifically static arrays we can take our knowledge 2 00:00:07,030 --> 00:00:12,310 a little bit further by talking about dynamic arrays which will grow or shrink to accommodate however 3 00:00:12,310 --> 00:00:15,770 much data we have at any point in our code. 4 00:00:15,770 --> 00:00:20,680 And the nice thing about dynamic arrays is if we wanted to add this fifth name if we wanted to add Eddie 5 00:00:20,980 --> 00:00:25,480 and we wanted to do it you know halfway through our code once we've already gotten a couple pages worth 6 00:00:25,480 --> 00:00:30,790 of code done instead of having to scroll back through and find where we declared our variable originally 7 00:00:31,150 --> 00:00:34,060 we could just read a mentioned our variable whenever we want. 8 00:00:34,060 --> 00:00:39,910 Meaning if our variable was originally 0 2 4 we could change it halfway through the code to be 0 to 9 00:00:39,910 --> 00:00:40,450 5. 10 00:00:40,450 --> 00:00:43,870 And without having to come back through and change our existing items. 11 00:00:43,990 --> 00:00:52,440 So as an example of this let's create another public sub this time we'll call it dynamic array and this 12 00:00:52,440 --> 00:01:00,840 time we'll call our variable students dynamic so that we can see this is our dynamic array and we'll 13 00:01:00,840 --> 00:01:02,210 tell it it's a string. 14 00:01:02,260 --> 00:01:08,530 Again you can see right off the bat whereas we originally put a sizing in this original declaration 15 00:01:08,530 --> 00:01:10,520 the static declaration up here. 16 00:01:10,620 --> 00:01:14,790 We have not put a sizing in our parentheses for our dynamic array. 17 00:01:15,240 --> 00:01:21,940 So all we told the system this time is in our original declaration here's the name of our array. 18 00:01:22,050 --> 00:01:26,070 We can tell it's an array because we have the parentheses here but we haven't given you a size. 19 00:01:26,070 --> 00:01:28,230 This could be any size that we want it to be. 20 00:01:28,410 --> 00:01:34,830 And it is string which means text and this is different from our static declaration where we said here's 21 00:01:34,830 --> 00:01:35,930 our array name. 22 00:01:35,970 --> 00:01:39,150 Here's the size of the array and it is text. 23 00:01:39,150 --> 00:01:45,110 So this time we can resize this array to be whatever we want it to be and to resize things. 24 00:01:45,120 --> 00:01:50,850 You re dim them you reader mentioned them so we'll go ahead and read amend. 25 00:01:50,960 --> 00:01:58,940 Reader mentioned this as an example and we'll read I mentioned this to hold up to fourth item you don't 26 00:01:58,940 --> 00:02:01,660 technically need to write the add string at the end of this. 27 00:02:01,670 --> 00:02:05,720 And that's because you already told the system that this array is a string. 28 00:02:05,870 --> 00:02:11,210 All that we're doing and read em is resizing it which means the only thing that should change is this 29 00:02:11,210 --> 00:02:12,230 value here. 30 00:02:12,350 --> 00:02:16,680 So we can actually take this off and hit enter. 31 00:02:17,160 --> 00:02:23,460 And now our dynamic array should hold between the zero and the fourth item so we can go ahead and set 32 00:02:23,460 --> 00:02:28,530 that up the zero item will be John 33 00:02:31,340 --> 00:02:40,190 the first item will be James The second item will be Hannah 34 00:02:44,340 --> 00:02:46,170 third item Connor 35 00:02:52,290 --> 00:03:00,300 fourth item Christina and I'll go ahead and set up the fifth item but I'm going to do it with an apostrophe 36 00:03:00,300 --> 00:03:05,640 in front of the line so that it is a comment line and doesn't affect the code at all the code was actually 37 00:03:05,640 --> 00:03:12,090 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 38 00:03:12,090 --> 00:03:15,930 to create the same input box this time I'll use X as my variable 39 00:03:19,860 --> 00:03:24,500 we'll ask the same question Which student number do you want to view. 40 00:03:26,190 --> 00:03:31,270 And we'll title it student I.D. again to this should interact with the user in the exact same way that 41 00:03:31,270 --> 00:03:37,060 our other coded and we'll create the same output box at the end. 42 00:03:37,070 --> 00:03:52,110 This little message box that says you selected and our students dynamic array variable X and we'll call 43 00:03:52,130 --> 00:03:57,480 the title of the Spock student name just like we did with the other one so these two pieces of code 44 00:03:57,870 --> 00:04:01,620 are the exact same for our message box for our input box. 45 00:04:01,620 --> 00:04:07,770 The only thing that's changed is this is a dynamic array because we haven't originally sized it. 46 00:04:07,950 --> 00:04:11,910 Whereas this is a static array because we did give it an original size. 47 00:04:12,030 --> 00:04:18,190 So just to get an example of how this runs make sure that this is working OK we'll go ahead and run 48 00:04:18,190 --> 00:04:18,860 this code. 49 00:04:18,910 --> 00:04:26,150 So we'll go to macros select dynamic array this time and click Run says which student number do you 50 00:04:26,150 --> 00:04:27,100 want to view. 51 00:04:27,140 --> 00:04:34,600 We'll say we want to view number one and we see it selects James so that we know this is working correctly. 52 00:04:34,700 --> 00:04:43,090 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 53 00:04:43,090 --> 00:04:43,720 fifth item. 54 00:04:43,720 --> 00:04:50,580 Now we know that this probably won't work because we size this to be four items long but we'll try to 55 00:04:50,580 --> 00:04:51,600 run it anyways. 56 00:04:51,600 --> 00:04:58,440 We'll go macros dynamic array run and we see it is going to give us an error and we can click debug 57 00:04:58,470 --> 00:05:02,350 and see there is occurring on the same exact one. 58 00:05:02,370 --> 00:05:07,980 Now the difference here is rather than coming back up to the beginning to our original declaration and 59 00:05:07,980 --> 00:05:11,630 resizing it there we can continue to resize this array. 60 00:05:11,640 --> 00:05:17,700 However many times we need to without changing the original declaration which means if I type read him 61 00:05:19,070 --> 00:05:28,090 students dynamic 0 to 5 here then I will have resize my variable to contain a fifth item. 62 00:05:28,100 --> 00:05:31,200 In this case we're going to say the fifth item is Eddie. 63 00:05:31,370 --> 00:05:37,760 And so when I go back to run this we should see that the fifth item is held correctly so we'll highlight 64 00:05:37,760 --> 00:05:43,510 dynamic array and we'll click Run we see everything seems to be working OK we'll put an item number 65 00:05:43,510 --> 00:05:51,700 five and click ok and we see it has selected Eddie so we know we have correctly re sized our array variable 66 00:05:51,700 --> 00:05:53,590 to contain that fifth item. 67 00:05:53,590 --> 00:06:03,480 Now one thing that you'll notice is if we try to run this and find our second name you see it returns 68 00:06:03,480 --> 00:06:04,640 nothing. 69 00:06:04,680 --> 00:06:09,510 And the reason for this is when you read size your dynamic variable. 70 00:06:09,510 --> 00:06:13,230 Be careful about where your read resizing it and how your resizing it. 71 00:06:13,620 --> 00:06:19,950 So in this case we've re sized our variable after we've already assigned the zero through fourth item 72 00:06:20,520 --> 00:06:26,250 and that means that all of this data that we've previously assigned right now it's being dropped and 73 00:06:26,250 --> 00:06:32,080 we don't necessarily want that data to be dropped and there's a way to prevent that from happening. 74 00:06:32,100 --> 00:06:41,010 And so this is preserve so we'll type the word preserve out in front of our resized variable declaration 75 00:06:41,010 --> 00:06:41,780 here. 76 00:06:41,850 --> 00:06:49,710 So instead of just reading resize my variable I want it now to be this variable to be this long we're 77 00:06:49,710 --> 00:06:55,150 going to say resize our variable but preserve the values that are already in my variable. 78 00:06:55,260 --> 00:06:56,910 And this is how long I want it to be. 79 00:06:57,300 --> 00:07:03,100 So now that I've put preserve here this data should not be dropped when I resize my array instead. 80 00:07:03,240 --> 00:07:09,180 This this data will all be kept in the array and all that we'll be doing is adding a fifth item spot 81 00:07:09,180 --> 00:07:12,780 which we turn around and immediately assign the value of Eddie. 82 00:07:12,780 --> 00:07:14,190 So now when I try to run this 83 00:07:18,000 --> 00:07:25,270 and I again look for my second value we can see it has correctly held all of that data with it whereas 84 00:07:25,270 --> 00:07:27,860 it originally dropped all of that data. 85 00:07:27,940 --> 00:07:32,980 So we have preserved that data as we were resizing our dynamic array. 86 00:07:33,610 --> 00:07:39,680 So this has some some great implications for if you don't know what size array you need to begin with. 87 00:07:39,820 --> 00:07:43,750 Here's an array type that you can continue to add items to as needed. 88 00:07:43,780 --> 00:07:46,510 You can preserve all your data as you're adding items. 89 00:07:46,510 --> 00:07:51,370 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 90 00:07:51,370 --> 00:07:54,810 list a dynamic array is the way to go. 91 00:07:54,970 --> 00:07:59,770 But this does kind of beg question if we're going to continue adding items to my list can I find out 92 00:07:59,800 --> 00:08:05,700 how many items I have at the end of my list and there are ways to calculate that. 93 00:08:05,710 --> 00:08:09,820 And that is using the upper and lower bounds of the system. 94 00:08:09,820 --> 00:08:14,980 And in this case the upper bound is this five right here it's whatever the item number is that is the 95 00:08:14,980 --> 00:08:21,750 maximum of this array the lower bound is this number here it's whatever the lowest item number is. 96 00:08:21,850 --> 00:08:23,630 That can be within that array. 97 00:08:23,680 --> 00:08:31,750 So if we wanted to for example find out what the upper bound is we could create this I integer variable 98 00:08:32,320 --> 00:08:42,090 like so that we could set ie equal to the U bound which is the upper bound of students dynamic and hit 99 00:08:42,090 --> 00:08:43,070 enter. 100 00:08:43,260 --> 00:08:47,940 We could go ahead and create a little message box right here at the end to just tell us what the upper 101 00:08:47,940 --> 00:08:49,030 bound is. 102 00:08:49,140 --> 00:08:54,510 And if I run this we should see the number five pop up in the secondary message box. 103 00:08:54,510 --> 00:08:56,970 So I'll go ahead and do that as an example. 104 00:08:56,970 --> 00:09:00,890 Macros dynamic array run. 105 00:09:01,020 --> 00:09:05,730 And this doesn't matter we'll just say we want to view the fourth name which should be Christina. 106 00:09:05,730 --> 00:09:07,700 So this part is checking out okay. 107 00:09:07,830 --> 00:09:12,000 Now when we click OK we should see the number five pop up which it does. 108 00:09:12,030 --> 00:09:18,240 So this means our system has correctly counted the upper bound of our array and we could do the same 109 00:09:18,240 --> 00:09:19,410 thing with our lower bound. 110 00:09:19,410 --> 00:09:26,490 So if instead of the upper bound which is you would change this to El bound which is lower bound we 111 00:09:26,490 --> 00:09:32,910 should see zero show up in that system and you can use this at a multiple different ways we could change 112 00:09:32,910 --> 00:09:45,560 this to be you bound minus the El bound students dynamic so if you had a this dimension from 2 to 5 113 00:09:45,890 --> 00:09:50,960 then this would only retrieve you know you have three different items that you can store data in and 114 00:09:50,990 --> 00:09:56,480 this particular array that you've created there's lots of different ways to use the upper and lower 115 00:09:56,480 --> 00:10:02,240 bound of the system but this is an example of how you might use this to count the number of items that 116 00:10:02,240 --> 00:10:09,770 you can put into your array in this video we cover dynamic arrays and how they can help with unknown 117 00:10:09,770 --> 00:10:15,920 sizes of data and the next video we're going to talk about creating multi-dimensional arrays which we 118 00:10:15,920 --> 00:10:18,860 can use to cover larger ranges of data all at once.