FAQ: Basic Classes and Objects - Properties

This community-built FAQ covers the “Properties” exercise from the lesson “Basic Classes and Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Properties

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: [REPORTED] Incorrect variable declaration in C# lesson on Basic Classes and Objects - Properties

Why doesn’t my answer work

using System;

namespace BasicClasses
{
class Forest
{
public string name;
public int trees;
public int age;
public string biome;

public string Name
{
  get { return name; }
  set { name = value; }
}

public int Trees
{
  get { return trees; }
  set { trees = value; }
}

public string Biome
{
  get { return biome; }
  set
  { 
    if (value != "Tropical" || value != "Temperate" || value != "Boreal") { biome = "Unknown"; }
    else { biome = value; }
  }
}

}

}

That appears OK? What about Program.cs?

Are you seeing some feedback from the automatic grader? Is it giving some error or is it telling you that you are failing some test case?

I don’t think your if condition in the setter for Biome is working as intended. You need to rethink the logic.
Suppose we tried to set the value “Tropical” for Biome, then in your if condition, value != "Tropical" will evaluate to False. Then, it will check the second operand value != "Temperate" which will evaluate to True. Therefore, it will set biome = "Unknown" which is not the correct outcome.

Similarly, if we tried to set the value “Temperate” for Biome, then in your if condition, value != "Tropical" will evaluate to True and it will set biome = "Unknown" which is not the correct outcome.

If you tried some other value like “Desert” for Biome, then in your if condition, value != "Tropical" will evaluate to True and it will set biome = "Unknown" which is the correct outcome but only because of faulty logic.

There are at least a couple of ways in which you can fix the logic. I can share those if you wish, but I am not doing so presently because perhaps you want to think some more of how to correct the logic yourself.

1 Like

His code is all but identical to mine and its functional so that is why I asked about Program.cs

@mtrtmk’s description of the problem is accurate. value cannot be equal to all three values: “Tropical”, “Temperate” and “Boreal”. Regardless of the value assigned to value, the if condition will always evaluate to true resulting in “Unknown” being assigned every single time. When using the || (or) operator, the first truthy value encountered from left to right ends the comparison and returns true.

1 Like

How is that different from what I did here that works?

public string Biome
    {
      get {return biome; }
      set 
      {
        switch (value)
        {
          case "Tropical": 
          case "Temperate": 
          case "Boreal": biome = value; break;
          default: biome = "Unknown"; break;
        }
      }
    }

All cases can evaluate to setting biome to value then break.

How is it at all the same? The combination of != and || in @dr_o’s code is not the same logic that you employed using switch. Run @dr_o’s code for yourself.

Was there any particular approach the lesson designer had in mind? I used this approach; `using System;

namespace BasicClasses
{
class Forest
{

public string name;   
  public string Name{
    get {return name;}
    set {name = value;}
  }

public int trees;
  public int Trees{
    get {return trees;}
    set {trees = value;}
  }

public int age;

public string biome;
  public string Biome{
    get {return biome;}
    set{
      switch (value) {
        case "Tropical" :
          biome = "Tropical";
          break;
        case "Temperate" :
          biome = "Temperate";
          break;
        case "Boreal" :
          biome = "Boreal";
          break;
        default : 
          biome = "Unknown";
          break;
      }
    }
  }

}

}
`
but would be interested in alternative or possibly even more aptly intended approaches. I would also much appreciate any formatting or general improvement advice.