Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We'll use the new Bird Search Extension to find the bird I saw.
-
0:00
We'll need to add the using directives for system.collections.generic and
-
0:05
system.link to this file.
-
0:09
Using System.Collections.Generic and using System.
-
0:20
Link.
-
0:23
Let's put our search feature to work and
-
0:25
try to figure out what the bird was that I saw the other day.
-
0:29
We can write a little console application that will cycle through each page of
-
0:33
the results until we press a certain key.
-
0:36
So let's hop over to the program.cs file.
-
0:41
First, let's create our search parameters.
-
0:45
var searchParameters= new BirdSearch.
-
0:52
So we'll use object initialization here.
-
0:59
And so the bird was Size = medium and
-
1:06
The country was the United States and
-
1:13
the colors we have to give
-
1:18
it a new list of string and
-
1:23
the colors were White,
-
1:27
Brown and Black.
-
1:32
Okay, and then the page will give it zero to start out.
-
1:40
And the page size will give it, what did I say earlier a five, all right.
-
1:49
So we've got our search parameters.
-
1:53
Let's write something to the console so they know what's going on.
-
1:56
Console.WriteLine Type any key to begin search.
-
2:03
Kinda like a go button, right?
-
2:09
All right, now we're gonna need a loop to loop through our results.
-
2:13
While Console.ReadKey,
-
2:18
so that'll read only one key at the console.
-
2:22
And it has a property called KeyChar.
-
2:26
And if that is not equal to q, we'll say.
-
2:33
Okay.
-
2:35
So this loop is going to keep looping and
-
2:38
reading a key from a console until somebody presses a q.
-
2:44
First, let's write out something to the console.
-
2:48
I'm going to use a new feature of c sharp instead of using
-
2:53
string.format like we have in previous videos.
-
2:56
I'm gonna use a dollar sign, then a string quote.
-
3:01
Inside here, I'm gonna say Page colon, and open close curly braces.
-
3:10
So when I put this dollar sign right here, inside the curly braces,
-
3:14
I can put in code.
-
3:16
So we'll use our searchParameters .Page.
-
3:21
So we're going to let whoever's at the console know what page they're on.
-
3:28
Then, I think we need to use our search on birds but
-
3:33
we don't have a list of birds so we'll create a new var
-
3:40
birds one last time = BirdRepository.LoadBirds.
-
3:49
Okay, and now we can call our search method .search and
-
3:54
we'll pass it the searchParameters search parameters And
-
4:00
then will just well call ToList put it in a list and
-
4:05
there's an other method I wanna show you called ForEach.
-
4:12
And what this does we can pass it on lambda.
-
4:16
goes to and what that does is run a piece of code for every element in the list.
-
4:24
So for each b in birds,
-
4:28
let's write out to the console, console.WriteLine.
-
4:32
And we'll do that magic string mashing thing again.
-
4:38
We'll start out with a dollar sign.
-
4:41
I like to go ahead and quote, quote end my method just so
-
4:44
I don't have any syntax errors.
-
4:46
So in here we're going to write the name of the bird Common Name
-
4:53
equals and then we need our curly braces.
-
4:55
So that's b.CommonName because our b Is our input parameter for a lambda.
-
5:06
All right, and we need to close out the for each method.
-
5:14
So while this is looping, it's not gonna change the page.
-
5:18
We need to change the page after every time we print them out to the screen.
-
5:23
So let's search parameters every time we loop through, we'll add to the page.
-
5:32
Page, plus plus.
-
5:32
So, we'll add one to the page property.
-
5:36
Okay.
-
5:38
So I think we can go ahead and compile this guy.
-
5:41
So we're going to compile on the console with mcs.
-
5:44
All of our files with star.cs.
-
5:47
Then we're gonna specify the out argument and we're gonna give it Program.exe.
-
5:56
So that it'll compile all our files and give us an executable called
-
6:00
Program.exe, we've got an error.
-
6:05
Bird search extension.cs.
-
6:07
Let's go check that out.
-
6:11
Line 22 oops looks like we've got a couple of typos here
-
6:16
where s.size and search should have been an a in it.
-
6:22
Did you catch that before I did?
-
6:23
All right, we'll save that file and we'll try it again.
-
6:30
Okay.
-
6:33
BirdSearchExtension, the name page does not exist.
-
6:38
That's line 29.
-
6:40
Ha.
-
6:41
I put in just random variable names here.
-
6:48
This should be search.Page and
-
6:54
this should be search.PageSize,
-
7:02
okay, and search.PageSize.
-
7:09
All right, let's try to compile again.
-
7:15
All right great, so now we can run
-
7:20
this with mono Program.exe.
-
7:24
Okay, type any key to begin search.
-
7:28
All right, there's our first page could be any of those.
-
7:31
[LAUGH] All right.
-
7:36
2, 3. It looks like we've run out of
-
7:38
bird results.
-
7:40
Now, I'll press q to exit.
-
7:43
Awesome.
You need to sign up for Treehouse in order to download course files.
Sign up