FAQ: Basic Classes and Objects - Properties

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

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.

1 Like

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;}
2 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

2 Likes

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

Thought I’d share my solution. Took me a while to get it working, but it’s short and only uses concepts already introduced:

public string Biome {
      get { return biome; }
      set{ biome = Array.Exists(new string[] {"Tropical","Temperate","Boreal"},s => s == value) ? value : "Unknown";
      }
    }
1 Like

The error seems to be gone, I don’t know what happened, if something like this happens to you, try to restart your navigator

I’m stuck at step 1 :
I’m getting an error which I think I can’t do much about

/usr/share/dotnet/sdk/3.1.421/NuGet.targets(128,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/home/ccuser/workspace/csharp-basic-classes-properties/BasicClasses.csproj]

I’m getting this error at the bottom of the program, not in the console.
Any ideas on why this is happening ?

Thanks