Reputation: 306
I am new to c# programming and I have a method as follows:
public void GuessTheHiddenDigits(List<int> list)
{
GuessingGame playerGuess = new GuessingGame();
if(playerGuess.Guesses == null){
playerGuess.Guesses = list;
}
playerGuess.Guesses = list;
}
I wish to add my List list to the following:
public List<int> Guesses { get; set; }
Would someone be able to explain to me why it isnt adding my list accordingly?
Would someone also be able to point me in the right direction?
EDIT 1: GuessingGame class is as follows
public class GuessingGame
{
public List<int> Target { get; set; }
public List<int> Guesses { get; set; } // I need this to be set during the GuessTheHiddenDigits
public List<Guess> ShowGuessesMade()
{
var listRange = new List<Guess>();
if (listRange != null)
{
return listRange;
}
return listRange;
}
public void NewGame()
{
throw new NotImplementedException();
}
public void GuessTheHiddenDigits(List<int> list)
{
GuessingGame playerGuess = new GuessingGame();
playerGuess.Guesses = new List<int>();
playerGuess.Guesses.AddRange(list);
}
}
Edit 2: unit test - my current public List Guesses is set to null, the list is getting passed in my method, debugging shows me that much. It just isn't setting the property.
[TestMethod]
public void GuessTheHiddenDigitsAddsTheSubmittedGuessToTheListOfGuesses()
{
var theGame = new GuessingGame();
/* NOTE : The next line forces us to add a behaviour to the GuessingGame
* class: the GuessTheHiddenDigits() method.
* */
theGame.GuessTheHiddenDigits(new List<int>() { 1, 2, 3 });
var theContext = new FakeHttpContext();
var theKey = "GameState";
theContext.Session.Add(theKey, theGame);
var controller = new Exercise09Controller();
var request = new System.Web.Routing.RequestContext(theContext, new System.Web.Routing.RouteData());
controller.ControllerContext = new System.Web.Mvc.ControllerContext(request, controller);
//Finally, set up the new guess
var theGuess = new List<int>() { 2, 3, 4 };
//Act
controller.GuessTheDigits(theGuess);
var result = controller.ShowPreviousGuesses();
var lastGuess = ((List<Guess>)result.Model).LastOrDefault(); //Returns null if the list is empty
//Assert
/* NOTE : This line forces another implementation decision: to use a
* C# property for Guess.Digits to represent the player's guess.
* */
CollectionAssert.AreEqual(theGuess, lastGuess.Digits);
}
Controller:
using Prigmore2013_01.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Prigmore2013_01.Tests
{
public class Exercise09Controller : Controller
{
//
// GET: /Exercise09/
public ActionResult Index()
{
return View();
}
public ViewResult ShowPreviousGuesses()
{
var model = new List<Guess>();
var m = new GuessingGame();
if(HttpContext.Session["GameState"] == null)
{
HttpContext.Session["GameState"] = new GuessingGame();
}
return View("Index", model);
}
public ViewResult ShowGuessesMade()
{
return View();
}
public ActionResult GuessTheDigits(List<int> theGuess)
{
GuessingGame theGame = this.Session["GameState"] as GuessingGame;
theGame.GuessTheHiddenDigits(theGuess);
return RedirectToAction("Index");
}
public RedirectToRouteResult StartNewGame()
{
return RedirectToRoute(new
{
controller = "Index",
action = "Index"
});
}
}
}
Upvotes: 0
Views: 534
Reputation: 23208
Your issue is that you are creating a new instance of GuessingGame
instead of altering the current this
instance of it in your method:
Change this:
public void GuessTheHiddenDigits(List<int> list)
{
GuessingGame playerGuess = new GuessingGame();
playerGuess.Guesses = new List<int>();
playerGuess.Guesses.AddRange(list);
}
Into this:
public void GuessTheHiddenDigits(List<int> list)
{
this.Guesses = new List<int>();
this.Guesses.AddRange(list);
}
Or more succinctly:
public void GuessTheHiddenDigits(IEnumerable<int> guesses)
{
this.Guesses = new List<int>(guesses);
}
Upvotes: 1
Reputation: 29244
This might confuse you, but is this what you want?
public struct Guess
{
public List<int> Digits { get; set; }
}
public class GuessingGame
{
List<Guess> guesses;
public GuessingGame()
{
guesses=new List<Guess>();
}
public void Guess(List<int> digits)
{
guesses.Add(new Guess() { Digits=digits });
}
}
Upvotes: 0