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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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.

3 Likes

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.

2 Likes

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.

1 Like

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.

The cleanest way I’ve though of for handling it is using this code for biome, which uses the Array.IndexOf method described in a previous lesson. By checking the array index of an arbitrary string, we indirectly find out whether the string exists within the array.

public string Biome {
    get {return biome;}
    set {
      string[] allowedBiomes = {
        "Tropical",
        "Temperate",
        "Boreal"
      };
      if (Array.IndexOf(allowedBiomes,value) >= 0) {
        biome = value;
      }
      else {
        biome = "Unknown";
      }
    }
  }

You could also use Array.Exists for an even more concise result, though it’s not mentioned.

3 Likes

I like your solution a lot - IMO an even cleaner way is to use a List, but it hasn’t been covered and you’d need to add a using for System.Collections.Generic:

using System.Collections.Generic;

private List<string> validBiomes = new List<string>(){"Tropical","Temperate","Boreal"};
private string biome;
public string Biome
{
    get { return biome; }
    set { biome = validBiomes.Contains(value) ? value : "Unknown"; }
}

Of course, I’d probably even take it a step further an make Biome an Enum field instead of a string, depending on the context, but that’s probably overkill - and hasn’t been taught yet :slight_smile:

I’d suggest changing != to == since the first if-statement block will run when one of these conditions returns true with the || operator.

set
{
    if (value == "Tropical" || value == "Temperate" || value == "Boreal")
    {
        biome = value;
    }
    else
    {
        biome = "Unknown";
    }
}

As @mtrtmk mentioned, value can’t equal to three different values.

In your situation, no matter what the value is the first block will always run:

if (false || true || true) { /* runs codes */ }
if (true || false || true) { /* runs codes */ }
if (true || true || false) { /* runs codes */ }
if (true || true || true) { /* runs codes */ }

Changing || to && will solve the problem if you prefer !=:

if (false && true && true) { /* ignores codes */ }
if (true && false && true) { /* ignores codes */ }
if (true && true && false) { /* ignores codes */ }
if (true && true && true) { /* runs codes */ }
7 Likes

I have no idea what just happened in this exercise. This is where I got lost:

The Area property is associated with the area field. It’s common to name a property with the title-cased version of its field’s name, e.g. age and Age , name and Name .

I managed to get this far, step 3:

using System;

namespace BasicClasses

{

class Program

{

static void Main(string[] args)

{

  Forest f = new Forest();

  f.Name = "Congo";

  f.Trees = 0;

  f.age = 0;

  f.biome = "Tropical";

  

  Console.WriteLine(f.name);

}

}

}

but it just gives an error, and I can’t get past the step:
Replace f.name in Program.cs.
Am I missing something?

1 Like

Have a look at your Console.WriteLine statement.

7 Likes

That’s embarrassing. I wrote the same thing at least 5 times… No wonder it wouldn’t work. Thanks for your reply.

1 Like

I was facing the same thing lol!

For me there seems to be a bug in which in step 3 it doesn’t recognize when i rename f.name to f.Name and f.trees to f.Trees

Look carefully at your code. Have you renamed ALL of them? In particular, have a close look at the Console.WriteLine statement in Program.cs.

1 Like

True! I got it a few moments after. Should probably look more closely before posting on the forums =P