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 Adding Repository Methods

mikkel pohjola
mikkel pohjola
1,966 Points

I am lost on challange 2 in videoGameRepository - mvc basic

I do not know where I fail, I've tried most solutions in the community, but where do I make mistakes in this one here ??

VideoGamesRepository.cs
using Treehouse.Models;

namespace Treehouse.Data
{
    public class VideoGamesRepository
    {
        // TODO Add GetVideoGames method
           public VideoGame[] GetVideoGames(int id)
        {
            VideoGame[] videoToReturn = null; 

            foreach (var _videoGame in _videoGames) 
            { 
                if(_videoGame.Id==id)
                {
                    videoToReturn = _videoGame; 
                    break;
                }
            }
            return _videoGames;
        }
        // TODO Add GetVideoGame method

        private static VideoGame[] _videoGames = new VideoGame[]
        {
            new VideoGame()
            {
                Id = 1,
                Title = "Super Mario 64",
                Description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.",
                Characters = new string[]
                {
                    "Mario",
                    "Princess Peach",
                    "Bowser",
                    "Toad",
                    "Yoshi"
                },
                Publisher = "Nintendo",
                Favorite = true
            },
            new VideoGame()
            {
                Id = 2,
                Title = "Mario Kart 64",
                Description = "Mario Kart 64 is a 1996 go-kart racing game developed and published by Nintendo for the Nintendo 64 video game console.",
                Characters = new string[]
                {
                    "Mario",
                    "Princess Peach",
                    "Bowser",
                    "Toad",
                    "Yoshi"
                },
                Publisher = "Nintendo",
                Favorite = false
            }
        };
    }
}
VideoGame.cs
namespace Treehouse.Models
{
    // Don't make any changes to this class!
    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 bool Favorite { get; set; }

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

5 Answers

Steven Parker
Steven Parker
229,732 Points

You're pretty close now.

  • but GetVideoGame should return just one object, not an array.
  • and the object to return is the one you pick in your loop (videoToReturn).
mikkel pohjola
mikkel pohjola
1,966 Points

the problem is solved, but do not ask me if I understood it, :D it was a difficult task, I looked at what James Churchill did in the video and that worked. Here is my solution

public VideoGame GetVideoGame(int id)
        {
            VideoGame videoToReturn = null;
            foreach (var _videoGame in _videoGames)
            {
                if (_videoGame.Id == id)
                {
                    videoToReturn = _videoGame;

                    break;
                }
            }

            return videoToReturn;
        }
Steven Parker
Steven Parker
229,732 Points

That's exactly what I was hinting at, looks good! :+1:

So what about it is confusing?

Steven Parker
Steven Parker
229,732 Points

Did you get the tasks confused?

Your method above is much more complicated than needed for task 1. Remember, the goals for the GetVideoGames method of task 1 are:

  • The GetVideoGames method shouldn't accept any parameters.
  • The GetVideoGames method should return the _videoGames private static field.

Then for task 2, you leave your task 1 method as it passed, and make a different method named GetVideoGame (with no "s"). The task 2 method will be much like the one you show above.

mikkel pohjola
mikkel pohjola
1,966 Points

now I understand it, I must have two methods I've changed in method 1 from the challenge 1 This is why it does not work :), i will try again :D

mikkel pohjola
mikkel pohjola
1,966 Points

I've created two methods, one is from challenge 1, it is not changed but then I made a new one to challenge 2 and it should return the Video Game from object _videoGames, but it does not work :(

using Treehouse.Models;

namespace Treehouse.Data
{
    public class VideoGamesRepository
    {
        // TODO Add GetVideoGames method
       //Method 1 from challange 1
        public VideoGame[] GetVideoGames()
        {
            return _videoGames;
        }



        //Method 2 from challange 2
        public VideoGame[] GetVideoGame(int id)
        {
            VideoGame videoToReturn = null; 

            foreach (var _videoGame in _videoGames) 
            { 
                if(_videoGame.Id==id)
                {
                    videoToReturn = _videoGame; 
                    break;
                }
            }
            return _videoGames;
        }

        // TODO Add GetVideoGame method

        private static VideoGame[] _videoGames = new VideoGame[]
        {
            new VideoGame()
            {
                Id = 1,
                Title = "Super Mario 64",
                Description = "Super Mario 64 is a 1996 platform video game developed and published by Nintendo for the Nintendo 64.",
                Characters = new string[]
                {
                    "Mario",
                    "Princess Peach",
                    "Bowser",
                    "Toad",
                    "Yoshi"
                },
                Publisher = "Nintendo",
                Favorite = true
            },
            new VideoGame()
            {
                Id = 2,
                Title = "Mario Kart 64",
                Description = "Mario Kart 64 is a 1996 go-kart racing game developed and published by Nintendo for the Nintendo 64 video game console.",
                Characters = new string[]
                {
                    "Mario",
                    "Princess Peach",
                    "Bowser",
                    "Toad",
                    "Yoshi"
                },
                Publisher = "Nintendo",
                Favorite = false
            }
        };
    }
}```
mikkel pohjola
mikkel pohjola
1,966 Points

it confuse me with all the video names, so you're saying that I should return the _videoGame from my loop :) , I have tried to return all the variables from the method public Videogame [] GetVideoGame (int id), but it does not work :)

it's a tough one this challenge :D