简体   繁体   English

枚举:接口无法声明类型

[英]Enum: interfaces cannot declare types

From one class, I'm trying to call an enum from another class. 从一个类,我试图从另一个类中调用一个枚举。 According to some solution that I found on this site, I had to create the enum inside an interface and then create a property from the class. 根据我在此站点上找到的一些解决方案,我必须在接口内创建枚举,然后从该类创建属性。 That works perfectly for me, but when I try to compile the project, I receive the error message: "enum_Name: 'interfaces cannot declare types'. 这对我来说非常理想,但是当我尝试编译项目时,收到错误消息:“ enum_Name:'接口无法声明类型'。

Here is the code for the interface: 这是接口的代码:

public interface IEnums
{

public enum enum_SomeName
{
    firstenumname = 1,
    secondenumname = 2,
    thirdenumname = 3,
    forthenumname = 4,
    fifthenumname = 5
}

Here is the class the property: 这是该类的属性:

  public class Whatever
 {
   public IEnums.SomeName wtf {get; set; }
 }

As I mentioned, I'm able to call the enum on the other class on 40 different methods, but I receive the error message on line: public enum enum_SomeName. 如前所述,我可以使用40种不同的方法在另一个类上调用该枚举,但是我在网上收到错误消息:public enum enum_SomeName。

Either you misunderstood that advice or it was wrong. 您可能误解了该建议,或者它是错误的。 It is legal, but rare to put an enum declaration inside a class or struct declaration. 这是合法的,但很少将枚举声明放在类或结构声明中。 It is not necessary and you should not do so without good reason. 这是没有必要的,没有充分的理由也不应这样做。 It is not legal to put an enum deck in an interface. 在接口中放置枚举程序集是不合法的。 Put it outside. 放在外面

You're trying to define the enum WITHIN the interface. 您正在尝试在接口内定义枚举。 You can't do that. 你不能那样做。

You can have the interface have a scalar value of the enum. 您可以使接口具有枚举的标量值。

public interface IEmployee
{

  Enums.EmployeeStatusEnum EmpStatus {get;set;}
}



public enum EmployeeStatusEnum 
{
    Unknown = 0,
    Hired= 1,
    Fired= 2
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM