简体   繁体   中英

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'.

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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