Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation with Properties Accessor Methods

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

i've already declared a method named SetNumFliesEaten

I already declared the method so why does it give me the error that I didn't declare it.

Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        private int _numFliesEaten;

        public int GetNumFliesEaten
        {
            get
            {
                return _numFliesEaten;
            }

         }
      public int SetNumFliesEaten
      {
       set
            {
                _numFliesEaten = value;
            }

      }
   }
}

8 Answers

Steven Parker
Steven Parker
229,644 Points

Did you peek ahead at the next lesson? It looks like you're mixing method syntax with property syntax.

For this challenge, just create simple methods. The definitions must have parentheses, and the setter method will require an argument.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

i have watched the video though I don't see how i converted the syntax to property syntax

Steven Parker
Steven Parker
229,644 Points

Property declarations don't have parentheses, and inside them you have accessors with the names "get" and "set". So the code shown above appears to have two partially-implemented properties.

On the other hand, accessor method declarations would have parentheses (with or without arguments) after their names, and the body would contain the code itself.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

yes I know that though why would it ask me to write the setnumflies method again if it's clear that i already done that

Steven Parker
Steven Parker
229,644 Points

What you have here is a "SetNumFliesEaten" property, but the instructions ask for a "SetNumFliesEaten" method.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

just to check is the first method in property form

Steven Parker
Steven Parker
229,644 Points

There are no methods in the code shown above, but two incomplete properties. Method definitions would have parentheses.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

just to see, could you type out the correct parentheses for the method syntax so I don't mix up with property syntax, parentheses is the only thing that confuses me on the keyboard

Steven Parker
Steven Parker
229,644 Points

A generic "getter" method might look like this:

        public int GetSomeLocalValue()   // <-- methods have parentheses, even if empty
        {
            return _someLocalValue;
        }
Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

ok thanks it makes life easier for me :D I don't understand why it gives the error: Frog.cs(10,12): error CS1525: Unexpected symbol {' Frog.cs(18,12): error CS1525: Unexpected symbol{' Compilation failed: 2 error(s), 0 warnings

''' namespace Treehouse.CodeChallenges { public class Frog { private int _numFliesEaten;

    public int GetNumFliesEaten()
    {
        get
        {
            return _numFliesEaten;
        }

    }
  public int SetNumFliesEaten()
  {
   set
        {
            _numFliesEaten = value;
        }

  }

} } '''

Steven Parker
Steven Parker
229,644 Points

Take a closer look at the sample getter method I wrote, you'll see it contains code directly, with no inner "get" block. That's also something only used on properties. Also:

  • setter methods need to take an argument
  • setter methods normally don't return anything (return type "void")
Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

i have removed the return syntax and the getter method though it gives me the error : Frog.cs(18,9): error CS1525: Unexpected symbol `{' Compilation failed: 1 error(s), 0 warnings

Steven Parker
Steven Parker
229,644 Points

You must be pretty close by now. Try exercising your debugging skills. An unexpected symbol is often caused by something missing that should be in front of it.

This thread seems to have gone far afield of the original question. If you give it your best shot and still haven't resolved it, please close out this question and start a new one showing what your current code looks like there.