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

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;}
3 Likes

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";
      
      Console.WriteLine(f.Biome);
    }
  }
}

1 Like

I FOUND MY ISSUE WITH THIS!
I wrote
if (biome == "Tropical")
When its supposed to be
if (value == "Tropical")

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

1 Like

PLEASE READ IF YOU’RE CONFUSED ABOUT SET, GET, AND VALUE.

I don’t know about you guys but I was really confused about this lesson until it just clicked in my head. I’ll explain.
But first here’s my code

This is Program.Cs

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 = "taco";
      
      Console.WriteLine(f.Biome);
    }
  }
}

This is Forest.Cs

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 = value; }
          else { biome = "Unknown"; }
      }
    }
  }

}

So the first thing I was confused about was what value equaled and how the property is called in the first place from just getting the variable/fields. So in Program.Cs instead of calling the field you call the property and when you call it value equals whatever you put in the curly will equal the value in the property. I don’t know if I made any sense but thank you for your time

2 Likes