Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Querying With LINQ Now You're Querying Object Initialization

Daniel Hildreth
Daniel Hildreth
16,170 Points

Treehouse Complier Giving Error Code When Calling Bird Objects

I have no clue what I'm doing wrong. I have followed the video along to the "T", but I am getting a compiler error within the Treehouse Workspaces when running the code. At the end of the video I'm supposed to call the foreach loop on bird.Name, yet keep getting the error message you see at the bottom of my code. I even tried Bird.Name, but that also didn't work. What am I doing wrong here? Here is my code:

public class Bird
      > {                                                                                                                              
      >     public string Name { get; set; }                                                                                           
      >     public string Color { get; set; }                                                                                          
      >     public int Sightings { get; set; }                                                                                         
      > }                                                                                                                              
csharp> List<Bird> birds = new List<Bird> {
      > new Bird { Name = "Cardinal", Color = "Red", Sightings = 3 },
      > new Bird { Name = "Dove", Color = "White", Sightings = 2 } };
csharp> birds                                                                                                                          
{ Bird, Bird }                                                                                                                         
csharp> birds.Add(new Bird { Name = "Robin", Color = "Red", Sightings = 5 });                                                          
csharp> Bird blueJay = new Bird();
csharp> blueJay.Name = "Blue Jay";                                                                                                     
csharp> blueJay.Color = "Blue";                                                                                                        
csharp> blueJay.Sightings = 1;                                                                                                         
csharp> birds.Add(blueJay);                                                                                                            
csharp> foreach (Bird bird in birds)                                                                                                   
      > {                                                                                                                              
      >     Console.WriteLine(Bird.Name);                                                                                              
      > }                                                                                                                              
(3,25): error CS0120: An object reference is required to access non-static member `Bird.Name'
csharp> foreach(Bird bird in birds)                                                                                                    
      > {                                                                                                                              
      >     Console.WriteLine(bird.(1,21): error CS0103: The name `bird' does not exist in the current context

It's supposed to be this:

public class Bird
      > {                                                                                                                              
      >     public string Name { get; set; }                                                                                           
      >     public string Color { get; set; }                                                                                          
      >     public int Sightings { get; set; }                                                                                         
      > }                                                                                                                              
csharp> List<Bird> birds = new List<Bird> {
      > new Bird { Name = "Cardinal", Color = "Red", Sightings = 3 },
      > new Bird { Name = "Dove", Color = "White", Sightings = 2 } };
csharp> birds                                                                                                                          
{ Bird, Bird }                                                                                                                         
csharp> birds.Add(new Bird { Name = "Robin", Color = "Red", Sightings = 5 });                                                          
csharp> Bird blueJay = new Bird();
csharp> blueJay.Name = "Blue Jay";                                                                                                     
csharp> blueJay.Color = "Blue";                                                                                                        
csharp> blueJay.Sightings = 1;                                                                                                         
csharp> birds.Add(blueJay);                                                                                                            
csharp> foreach (Bird bird in birds)                                                                                                   
      > {                                                                                                                              
      >     Console.WriteLine(bird.Name);                                                                                              
      > }                                                                                                                              
Cardinal
Dove
Robin
Blue Jay
csharp>

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel like this part in your code:

foreach (Bird bird in birds)                                                                                                   
       {                                                                                                                              
           Console.WriteLine(Bird.Name);                                                                                              
       }                                                                                                                              

Should be:

foreach (Bird bird in birds)                                                                                                   
       {                                                                                                                              
           Console.WriteLine(bird.Name);                                                                                              
       } 

Remember that the current bird we're getting the name of is lower case. The class Bird is named "Bird". Make that change and see if it works! Hope this helps! :sparkles:

Daniel Hildreth
Daniel Hildreth
16,170 Points

Jennifer that way was the first way I tried doing that was (bird.Name). Everytime I'd hit the "." it's say it's not in the current context. If you look at my code at the bottom in the first section it has me doing it that way. However, it gave the following error code in it. I showcased both ways I tried doing which was (Bird.Name) and (bird.Name).

Console.WriteLine(bird.(1,21): error CS0103: The name `bird' does not exist in the current context
William Schultz
William Schultz
2,926 Points

Daniel, the reason you got the error when you typed the '.' is because bird doesn't exist outside of the for loop. Rewrite your for loop with the small letter 'b' in bird and should be fine.

Yes, I know this is 5 months old, but it helps me learn to answer these questions. ;-)