這裏的問題是,當我調用變量「遊戲」它返回和未分配的變量編譯錯誤。據我瞭解,當我將新值賦給一個變量時,它意味着初始化該變量。接收錯誤「使用本地未分配的變量」
public class GameController : Controller
{
// GET: Game
public ActionResult Index()
{
Random rnd = new Random();
int pins = rnd.Next(1, 10);
Games game = new Games()
{
frames = game.frames,
Pins = game.Pins,
Score = game.Score,
player = game.player,
};
return View();
}
}
}
模型
namespace webBowlingProject.Models
{
public class Games
{
public int frames { get; set; }
public int Score { get; set; }
public int Pins { get; set; }
public string player { get; set; }
}
}
您正在使用...在同一實例中的屬性將值分配給新遊戲實例的屬性?你想做什麼?你打算爲'game.frames','game.Pins'等來自另一個實例嗎? –
他們來自一個模型,意圖是實例化它們,然後在視圖中使用它們來捕獲和存儲關於「遊戲」的信息。 – Joshua