我是c#编程的新手,我有一个方法如下:
public void GuessTheHiddenDigits(List<int> list)
{
GuessingGame playerGuess = new GuessingGame();
if(playerGuess.Guesses == null){
playerGuess.Guesses = list;
}
playerGuess.Guesses = list;
}
我希望将我的列表列表添加到以下内容中:
public List<int> Guesses { get; set; }
有人能够向我解释为什么不相应地添加我的列表?
有人也能指出我正确的方向吗?
编辑1:GuessingGame类如下
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);
}
}
编辑2:单元测试 - 我当前的公共List Guesses设置为null,列表在我的方法中传递,调试显示了那么多。它只是没有设置属性。
[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);
}
控制器:
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"
});
}
}
}
答案 0 :(得分:2)
那将是
playerGuess.Guesses.AddRange( list );
答案 1 :(得分:1)
您的问题是,您正在创建GuessingGame
的新实例,而不是在方法中更改其当前this
实例:
改变这个:
public void GuessTheHiddenDigits(List<int> list)
{
GuessingGame playerGuess = new GuessingGame();
playerGuess.Guesses = new List<int>();
playerGuess.Guesses.AddRange(list);
}
进入这个:
public void GuessTheHiddenDigits(List<int> list)
{
this.Guesses = new List<int>();
this.Guesses.AddRange(list);
}
或者更简洁:
public void GuessTheHiddenDigits(IEnumerable<int> guesses)
{
this.Guesses = new List<int>(guesses);
}
答案 2 :(得分:0)
这可能会让你感到困惑,但这就是你想要的吗?
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 });
}
}