C#变量已分配但从未使用过

时间:2016-03-14 11:03:03

标签: c# visual-studio

我的代码出错了。我是C#的新手,我正在尝试制作一款基于双人游戏的游戏。使用visual studio执行此操作

错误是“警告2已分配变量'gs',但从不使用其值”

代码如下;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace GridWorld
{
public class newAI : BasePlayer
{
    PlayerWorldState myWorldState;

    private double maxSpeed = 5f;
    //public int MaxNumTurn = 15;
    private int Steps;
    private object count;


    public newAI ()
        : base()
    {
        this.Name = "AI FOR GAMES THE A TEAM";
        // this.MaxNumTurn = 15;
        var gs = new GridSquare.ContentType(); // the variable gs is now a new GridSquare content type

    }


    void Pathfind()
    {

        PlayerWorldState startingPoint = BasePlayer(GridSquare.ContentType.Snail);
        int myX = 0;
        int myY = 0;

        if (myX == -1 || myY == -1)     // if myX & myY are equal to minus 1 then increment the steps
            Steps++;
        {
            return;

        }

        gs [myX, myY].Steps = 0;     // outside whileloop 

        while (true)
        {
            bool madeProgress = false;

            foreach (startingPoint mainPoint in gs)
            {
                int x = mainPoint.X;
                int y = mainPoint.Y;

                if (SquareOpen(x, y))
                {
                    int passHere = GridSquare[x, y].Steps;

                    foreach (startingPoint movePoint in ValidMoves(x, y))
                    {
                        int myX = movePoint.X;
                        int myY = movePoint.Y;
                        int newPass = passHere + 1;

                        if (GridSquare[myX, myY].Steps > newPass)
                        {
                            GridSquare[myX, myY].Steps = newPass;
                            madeProgress = true;
                        }

2 个答案:

答案 0 :(得分:0)

这不是一个错误。正如它所说,它只是一个警告,表明变量'gs'被分配到某处但从未在任何地方使用过。这不应该导致您的应用程序停止工作。

答案 1 :(得分:0)

您在构造函数gs中声明并定义newAI并尝试在Pathfind中使用它。 Pathfind无法看到它,因为它是您的类构造函数的 local ,即本地范围的变量。将其移至您的班级,如maxSpeedStepscount变量。

这只是一个警告,但你的代码至少会有不确定的行为。

相关问题