FAQ: Basic Classes and Objects - Properties

I just wasted hours trying to teach myself something because CodeCademy made it unclear, so hopefully this will help someone else avoid wasting their time, too. By following the instructions, we are allowed to the next step in the lesson, but we cannot get the terminal to return “Unknown,” leading us to believe we haven’t actually learned to do what they’re teaching us. What they don’t tell us is that we will continue to experience that error until we reach and follow step 6: “Public vs. Private,” which shows us how to finally make our actions in this step work. So… be prepared for it to seem like you’re proceeding without really understanding, even though you actually have done it correctly.

Is it possible to use an array with if statement?

Tried it with below code and fail miserably.

There are many ways to use any array for this task.
One way would be something like (skipping over many statements, just giving the basic gist):

using System;
using System.Linq;

string[] bio = {"Tropical", "Temperate", "Boreal"};
if (bio.Contains(value)) {biome = value;}
else {biome = "Unknown";}

Another way would be something like:
int pos = Array.IndexOf(bio, value);
If value is not contained in the bio array, then pos will be -1. If it is in the array, then pos will be the index of the match.
So, your condition would be:

if (pos == -1) {biome = "Unknown";}
else {biome = value;}
1 Like

Yes you can get it to return “Unknown”. You have to call the property Biom, not field biom, and you have to enter a value that will result in “Unknown” output. Like this:

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 = "Desert";

I wrote
if (biome == "Tropical")
When its supposed to be
if (value == "Tropical")

I spent close to 30 minutes trying different things… Good night