Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Encapsulation and Arrays in C#!
You have completed Practice Encapsulation and Arrays in C#!
Preview
In this video, we'll review the solution to the third and final challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
How did you do?
0:00
Let's take a look at my solution.
0:01
In the MediaLibrary class,
0:03
I updated the GetItemAt method to
gracefully handle a bad index.
0:04
To do that,
I used the array's linked property
0:09
to determine if an index was available
before attempting to access it.
0:12
If ( then index, and then what we want to
do is check to see if the index is less
0:16
than the length of the array,
so items.Length).
0:24
Then, we can cut and paste this line
of code inside of our if statement.
0:33
We still need to handle the case when the
index is not less than the _items.Length
0:39
property.
0:43
So else, set of {}, and then here,
let's just return null.
0:44
This code as it's written will work fine,
but you could optionally use
0:53
the ternary if operator if you want
to make a little more concise.
0:57
Let's take a look at that.
1:00
We'll start with the return
keyword followed
1:04
by index < _items.Length then a ?,
1:11
then _items(index) a : and null.
1:17
Let's break this down.
1:26
This is our condition, so
1:27
if this condition evaluates the true,
this is the value that's returned.
1:29
Otherwise, this is
the value that's returned.
1:34
Sometimes adding parenthesis around
the condition makes this code a little bit
1:38
easier to read.
1:42
It's worth noting that some developers
like the clarity of this approach,
1:44
while others like the conciseness
of this approach.
1:49
Either way, it's fine to use.
1:53
So use the one that you or
your team prefers.
1:54
Then, over in the Program.cs file,
I updated the display and
1:57
DetectedMediaType methods to handle
a null item parameter value.
2:02
First, the Display method.
2:10
Right here at the beginning of the method,
I added an if statement.
2:13
So if item, Is null, Then just return.
2:17
This has the effect of short-circuiting
the method when the item parameter
2:25
is null.
2:29
I'll select and copy this code, scroll
down to the DetectMediaType method, and
2:30
paste it right at the beginning
of the method implementation.
2:34
And lastly, I saved, compiled, and ran
my program in order to test my changes.
2:40
And now, a call to the GetItemAt method,
with a non-existent index.
2:47
For a bad index, we turn snull,
2:53
which is in turn ignored by the display
and DetectMediaType methods.
2:56
It's worth pointing out that this solution
helps to fix the problem with non-existing
3:01
array indexes.
3:05
But, it doesn't provide any feedback as
to what happened during the execution of
3:06
the program.
3:10
One possibility, would be to simply write
a message to the console when a bad
3:14
index is passed to GetItemAt method.
3:19
To do this, I'm gonna switch back to
the less concise version of this code.
3:22
And here in the else statement,
I write System.Console.WriteLine,
3:28
And then the message.
3:37
An element at index, And
then the index parameter value.
3:40
Doesn't exist in the media library.
3:51
Save the file, and now,
when we compile and
3:57
run the program,
We see an appropriate error message.
4:00
An element at index 5 doesn't
exist in the media library.
4:06
Nice job completing this practice session.
4:11
Be sure to check out the next
session in this series
4:13
after you've completed the fifth
stage in the C# Objects Course.
4:16
Thanks for practicing with me,
and we'll see you next time.
4:20
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up