我的枚举存在范围问题
基本上,我有一个带有国际象棋游戏代码的课程, 它有一个枚举类..
namespace WindowsFormsApplication1
{
enum STATE { PROMOTION, CASTLING, CHECK, CHECKMATE, DONOTHING };
.....
我想从我的表单代码中引用它:这是代码:
namespace WindowsFormsApplication1
{
public partial class ChessBoard: Form
{
public STATE Gamestate { set; get; }
......
我得到这个:
错误 1 不一致的可访问性:属性类型“WindowsFormsApplication1.STATE”比属性“WindowsFormsApplication1.ChessBoard.Gamestate”更难访问 D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\ChessBoardGame\ChessBoardGame\ChessBoard .cs 15 22 国际象棋棋盘游戏
为什么会出现这种情况以及如何预防?
Basically, i have one class with a code of a chess game,
it has an enum class..
namespace WindowsFormsApplication1
{
enum STATE { PROMOTION, CASTLING, CHECK, CHECKMATE, DONOTHING };
.....
i want to refer to it from my Form code: here is the code:
namespace WindowsFormsApplication1
{
public partial class ChessBoard: Form
{
public STATE Gamestate { set; get; }
......
i get this:
Error 1 Inconsistent accessibility: property type 'WindowsFormsApplication1.STATE' is less accessible than property 'WindowsFormsApplication1.ChessBoard.Gamestate' D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\ChessBoardGame\ChessBoardGame\ChessBoard.cs 15 22 ChessBoardGame
Why do i get it and how can i prevent it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在
WindowsFormsApplication1
中将枚举声明为public
:You need to declare your enum as
public
inWindowsFormsApplication1
:首先,请不要在 C# 中使用 SCREAMING CAPS。使用 PascalCaseIdentifiers(或局部变量使用camelCaseIdentifiers);它们读起来更愉快。
其次,您可能会错过一些州。跟踪游戏是否处于允许捕获en passent的状态并不是一个坏主意。你也不会陷入僵局或其他“平局”的情况。
第三,解决您的实际问题:错误告诉您出了什么问题。 “State”和“GameState”的可访问性不一致。您省略了在 State 上放置可访问性修饰符,因此它获得了顶级枚举的“内部”默认可访问性。 “GameState”是公共类的公共财产。所以你说“这个可供公众使用的东西的类型是我的程序集的内部实现细节”,这显然看起来有点不可能,所以编译器会标记它。
我要问自己的第一个问题是“为什么课程是公开的?”您打算让该类之外的程序集创建此类吗?
如果您希望该类成为公共类,则:
如果您希望该类为内部类,则将类设置为内部。
First off, please don't use SCREAMING CAPS in C#. Use PascalCaseIdentifiers (or camelCaseIdentifiers for locals); they are much more pleasant to read.
Second, you might be missing some states. It's not a bad idea to track whether the game is in a state where an en passent capture is permitted. You also do not have a state for stalemate or other "drawn" situations.
Third, to address your actual question: the error is telling you what is wrong. "State" and "GameState" have inconsistent accessibility. You omitted to put an accessibility modifier on State, so it gets the default accessibility which is "internal" for a top-level enum. "GameState" is a public property of a public class. So you are saying "the type of this thing which is available to the public is an internal implementation detail of my assembly", which obviously seems a little bit impossible, so the compiler flags it.
The first question I would ask myself is "why is the class public?" Do you intend to have this class created by assemblies outside of this one?
If you intend the class to be public then:
If you intend the class to be internal then make the class internal.
即使它没有回答你的问题(因为这已经由@FreeAsInBeer完成),你真的应该考虑坚持一些更常见的命名约定并将默认值放在第一位。因此,结果可能类似于:
通过将
DoNothing
作为枚举的第一个成员,您可以将其声明为默认值 (0
)。因此,如果有人请求default(State)
,他只会得到DoNothing
,这对于默认值来说听起来相当不错。Even if it doesn't answer your question (cause this is already done by @FreeAsInBeer) you should really consider to stick to some more common naming convention and put the default value as first. So the result could look something like:
By putting
DoNothing
as first member of your enum you declare this as the default value (0
). So if someone asks for adefault(State)
he simply getsDoNothing
which sounds quite good for a default value.