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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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; }
}
}
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.
@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.
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.
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.
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
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 .