Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguity between variables in C#

Tags:

c#

I want to start by saying I did search first, and found a lot of similar issues on various other things, but not this problem exactly.

I have this code:

namespace New_Game.GameClasses
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Error : Microsoft.Xna.Framework.GameComponent
    {
        bool gameOver = false;
        List<Enemy> enemies = new List<Enemy>();

        public bool gameOver {
            get { return gameOver; }
            set { gameOver = value; }
        }

        public override void Update(GameTime gameTime, Vector2 target)
        {
            // TODO: Add your update code here
            Rectangle playerRect = new Rectangle((int)target.X, (int)target.Y, 64, 64);

            foreach (Enemy e in enemies)
            {
                e.Target = target;
                e.Update(gameTime);
                Rectangle enemyRect = new Rectangle((int)e.Position.X + 7, (int)e.Position.Y + 7, 32 - 7, 32 - 7);

                if (playerRect.Intersects(enemyRect))
                {
                    gameOver = true;
                }
            }
        }
    }
}

My problem comes in an error saying this: Ambiguity between 'New_Game.GameClasses.Error.gameOver' and 'New_Game.GameClasses.Error.gameOver'

If I remove the get/set method, I run into a different error when I try and access gameOver from my Game1.cs. If I change it to the following I get the same error:

public bool gameOver { get; set; }

My question, is how do I resolve the ambiguity errors?

like image 609
slooker Avatar asked Jan 17 '13 03:01

slooker


2 Answers

You need to rename your private gameOver variable. Change this:

bool gameOver = false;
public bool GameOver {
        get { return gameOver; }
        set { gameOver = value; }
    }

to

bool _gameOver = false;
public bool GameOver {
        get { return _gameOver; }
        set { _gameOver = value; }
    }

You can't use the same variable name in a single class.

Alternatively, assuming you're using a recent version of .Net, you could remove your private variable and just have:

public bool GameOver { get; set; }

Good luck.

like image 172
sgeddes Avatar answered Sep 27 '22 17:09

sgeddes


Name your private variable differently than your public one.

bool _gameOver = false;

public bool gameOver {
    get { return _gameOver; }
    set { _gameOver = value; }
}
like image 36
siger Avatar answered Sep 27 '22 17:09

siger