C#中变量之间的歧义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14371817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ambiguity between variables in C#
提问by slooker
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'
我的问题出现在一个错误中:“New_Game.GameClasses.Error.gameOver”和“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:
如果我删除 get/set 方法,当我尝试从 Game1.cs 访问 gameOver 时会遇到不同的错误。如果我将其更改为以下内容,则会出现相同的错误:
public bool gameOver { get; set; }
My question, is how do I resolve the ambiguity errors?
我的问题是如何解决歧义错误?
采纳答案by sgeddes
You need to rename your private gameOver variable. Change this:
您需要重命名您的私有 gameOver 变量。改变这个:
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:
或者,假设您使用的是最新版本的 .Net,您可以删除您的私有变量并只拥有:
public bool GameOver { get; set; }
Good luck.
祝你好运。
回答by Tommaso Belluzzo
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; }
}
回答by siger
Name your private variable differently than your public one.
将私有变量命名为与公共变量不同的名称。
bool _gameOver = false;
public bool gameOver {
get { return _gameOver; }
set { _gameOver = value; }
}
回答by Ileana Martínez González
In my case it was resolved renaming the classagregarPropiedad by agregarPropiedad2.
在我的情况下,解决了agregarPropiedad2重命名类agregarPropiedad 的问题。
.CS
。CS
public partial class agregarPropiedad2
Designer.CS
设计师.CS
public partial class agregarPropiedad2
ASPX
ASPX
Inherits="ProjectName.agregarPropiedad2"