C# ASP.NET MVC Basics Modeling and Presenting Data Adding Repository Methods

Mohammed Hossen
Mohammed Hossen
5,447 Points

Returning GetVideoGames objects?

        public VideoGame GetVideoGames()
        {
            VideoGame videoGameToReturn = null;
            foreach (var videoGame in _videoGames)
            {
                videoGameToReturn = videoGame;
            }
            return videoGameToReturn;
        }

Not sure what I am missing here

VideoGamesRepository.cs
using Treehouse.Models;

namespace Treehouse.Data
{
    public class VideoGamesRepository
    {
        // TODO Add GetVideoGames method
        public VideoGame GetVideoGames()
        {
            VideoGame videoGameToReturn = null;
            foreach (var videoGame in _videoGames)
            {
                videoGameToReturn = videoGame;
            }
            return videoGameToReturn;
        }
        // TODO Add GetVideoGame method
        public VideoGame GetVideoGame(int id)
        {
            VideoGame videoGameToReturn = null;
            foreach (var videoGame in _videoGames)
            {
                if (videoGame.Id == id)
                {
                    videoGameToReturn = videoGame;
                    break;
                }
            }
            return videoGameToReturn;
        }

        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 + ")";
            }
        }
    }
}

1 Answer

andren
andren
28,375 Points

You seem to be a bit confused about what the method is meant to do. The GetVideoGames method is just meant to return the _videoGames array, nothing more. With your current code you return the last VideoGame object in that array rather than the array itself.

The code required for the method is extremely simple, you just have to specify that you are returning an array of VideoGame objects and then return the _videoGames array.

Like this:

public VideoGame[] GetVideoGames() // Specify VideoGame[] as return type
{
    return _videoGames; // Return the `_videoGames` array
}
Mohammed Hossen
Mohammed Hossen
5,447 Points

Appreciate it but I already figured out the solution.