【发布时间】:2013-10-22 19:03:30
【问题描述】:
我正在尝试创建一个应该在其代码中包含这些变量的新类:
class Map
{
// Variable declaration
public int Width { get; set; } // Width of map in tiles
public int Height { get; set; } // Height of map in tiles
public int TileWidth { get; set; }
public int TileHeight { get; set; }
}
但由于某种原因,在 Game1.cs 中创建了一个新的 Map 类后,我似乎无法访问诸如 Width 和 Height 之类的东西。
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public static SpriteBatch spriteBatch;
// etc...
// Class initialization
Map map = new Map();
map.Width = 10; // Won't work, says it is a 'field' but used like a 'type'
}
我想我并没有尝试设置财产权,但我不确定如何实际这样做。
我在尝试上述操作时收到两条错误消息:
“Deep.Game1.Map”是一个“字段”,但用作“类型”
和
类、结构或接口成员声明中的标记“=”无效
【问题讨论】:
-
您需要在某个方法中使用“map.Width”。我敢打赌你是在课堂上的一个方法之外尝试的。
-
你确定你没有另一个名为
Map的类吗?也许您实际上是在创建另一个类实例? -
你有
map.Width w = 10;什么的吗?请务必包含您的确切代码和足够的代码,以便我们可以重现 same 错误。否则,我们将提供疯狂的猜测而不是合格的答案,并且无法有效地帮助您。 -
@EdS。我在第二个 sn-p 中添加了一些代码,以阐明代码在文件中的位置。
-
看来Servy得到了正确的答案。您不能在构造函数、方法或访问器之外以这种方式分配给
map的成员。