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# ASP.NET MVC Basics Modeling and Presenting Data Creating a Data Model

Josh Boersma
PLUS
Josh Boersma
Courses Plus Student 2,486 Points

Get for Readonly Property

Challenge is:

In VideoGame.cs add a read only property named DisplayText of type string. For the property's return value, return the Title property value followed by the Publisher property value in parentheses. For example, given a Title property value of "Super Mario 64" and a Publisher property value of "Nintendo", the property's return value should be: Super Mario 64 (Nintendo)

It just said there were compiler errors. I'm at work and can't see these. Can anyone give me some direction as how to properly define functions for a readonly property?

VideoGame.cs
namespace Treehouse.Models
{
    public class VideoGame{
        public int Id {get; set;}
        public string Title {get; set;}
        public string Description {get; set;}
        public string[] Characters {get; set;}
        public string Publisher {get; set;}
        public readonly string DisplayText {get{return Title + " (" + Publisher + ")";}; set;}

    }
}

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Josh,

In order to make a read only property you need to define only a getter like so:

public string DisplayText 
{
    get
    {
         return Title + " (" + Publisher + ")";
    }
}

I hope this helps.

Josh Boersma
Josh Boersma
Courses Plus Student 2,486 Points

I already tried that. It didn't (and doesn't) work. I tried including readonly in the property getter declaration but it still didn't work.

Justin Horner
Justin Horner
Treehouse Guest Teacher

Your code also includes some issues with semicolon placement. The code I posted does work, as I passed the challenge with it before hand. Here it is in full context so you can compare with what you have.

namespace Treehouse.Models
{
    public class VideoGame{
        public int Id {get; set;}
        public string Title {get; set;}
        public string Description {get; set;}
        public string[] Characters {get; set;}
        public string Publisher {get; set;}
        public string DisplayText 
        {
            get
            {
                return Title + " (" + Publisher + ")";
            }
        }
    }
}
Josh Boersma
Josh Boersma
Courses Plus Student 2,486 Points

Yours worked. Thanks. I assumed when they said it needed to be a read only property I assumed it meant actually use the 'readonly' in descriptor, as opposed to just not using a setter. Unfortunate that they assume the student is not as smart as they would like. Not the first time I have gotten code rejected not being what they wanted (read: Too advanced).