ambiguity between variables in C#

后端 未结 4 1792
无人及你
无人及你 2021-02-13 20:12

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:

         


        
相关标签:
4条回答
  • 2021-02-13 20:27

    Name your private variable differently than your public one.

    bool _gameOver = false;
    
    public bool gameOver {
        get { return _gameOver; }
        set { _gameOver = value; }
    }
    
    0 讨论(0)
  • 2021-02-13 20:44

    You can't have two components of your class using the same name. So change this:

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

    To this:

    private Boolean m_GameOver = false;
    
    public Boolean GameOver
    {
        get { return m_GameOver; }
        set { m_GameOver = value; }
    }
    
    0 讨论(0)
  • 2021-02-13 20:44

    In my case it was resolved renaming the class agregarPropiedad by agregarPropiedad2.

    .CS

    public partial class agregarPropiedad2
    

    Designer.CS

    public partial class agregarPropiedad2
    

    ASPX

    Inherits="ProjectName.agregarPropiedad2"
    
    0 讨论(0)
  • 2021-02-13 20:48

    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.

    0 讨论(0)
提交回复
热议问题