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 trialmikkel pohjola
1,966 PointsI 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 ??
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
}
};
}
}
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
231,198 PointsYou'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
1,966 Pointsthe 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
231,198 PointsThat's exactly what I was hinting at, looks good!
So what about it is confusing?
Steven Parker
231,198 PointsDid 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
1,966 Pointsnow 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
1,966 PointsI'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
1,966 Pointsit 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