简体   繁体   English

C#:从另一个类访问枚举

[英]C#: Access Enum from another class

I know I can put my enum at the Namespace area of a class so everyone can access it being in the same namespace.我知道我可以将枚举放在类的命名空间区域,这样每个人都可以在同一个命名空间中访问它。

// defined in class2
public enum Mode { Selected, New, }  

What I want is to access this enum from我想要的是从

public class1
{
   var class2 = new class2();
   // Set the Mode
   class2.Mode = Model.Selected
}

Is this somehow possible without using namespace area?如果不使用命名空间区域,这以某种方式可能吗?

You can declare an enum outside of a class:您可以在类之外声明一个枚举:

namespace MyNamespace
{
    public enum MyEnum
    {
        Entry1,
        Entry2,
    }
}

And then you can add using MyNamespace;然后你可以using MyNamespace;添加using MyNamespace; where it needs to be used.需要用到的地方。

Aaron's answer is very nice but I believe there is a much better way to do this: Aaron 的回答非常好,但我相信有更好的方法来做到这一点:

public static class class1
{
    public void Run()
    {
        class2.Mode mode = class2.Mode.Selected;

        if (mode == class2.Mode.Selected)
        {
            // Do something crazy here...
        }
    }
}

public static class class2
{
    public enum Mode
    { 
        Selected, 
        New
    } 
}

No point over complicating this.没有必要把这复杂化。 It is a simple task.这是一项简单的任务。

All the Best祝一切顺利

Chris.克里斯。

Yes:是:

class2.Mode = class2.Mode.Selected

But note that you can't have a nested type defined that has the same name as one of the outer class' members, so this code will not compile.但请注意,您不能定义与外部类成员之一同名的嵌套类型,因此此代码将无法编译。 Either the enum or the property will need to be named something else.枚举或属性都需要命名为其他名称。 Your class name and variable name conflict too, making this a bit more complex.您的类名和变量名也冲突,使这有点复杂。

To make this a more generic answer, if you have this:为了使它成为一个更通用的答案,如果你有这个:

public class Foo
{
    public SomeEnum SomeProperty { get; set; }

    public enum SomeEnum {
        Hello, World
    }
}

Then this code will assign an enum value to the property:然后此代码将为该属性分配一个枚举值:

Foo foo = new Foo();
foo.SomeProperty = Foo.SomeEnum.Hello;

If you are trying to do what is described below it will not work...如果您尝试执行下面描述的操作,它将无法正常工作...

    public class MyClass1
    {
        private enum Mode { New, Selected };
        public Mode ModeProperty { get; set; }
    }

    public class MyClass2
    {
        public MyClass2()
        {
            var myClass1 = new MyClass1();

            //this will not work due to protection level
            myClass1.ModeProperty = MyClass1.Mode.
        }
    }

What you could do however is below, which will work...然而,你可以做的是下面,这将工作......

    public interface IEnums
    {
        public enum Mode { New, Selected };
    }

    public class MyClass1
    {
        public IEnums.Mode ModeProperty { get; set; }
    }

    public class MyClass2
    {
        public MyClass2()
        {
            var myClass1 = new MyClass1();

            //this will work
            myClass1.ModeProperty = IEnums.Mode.New;
        }
    }

I ended up solving my issue by changing it to a namespace accessor, found by utilizing Intellisense.我最终通过将其更改为使用 Intellisense 找到的命名空间访问器来解决我的问题。 I thought the enum was in the class, not just the namespace.我认为枚举在类中,而不仅仅是命名空间。 If it was in the class, I would recommend moving it out of the class.如果它在课堂上,我会建议将其移出课堂。

namespace ABC.XYZ.Contracts
{
  public class ChangeOrder : BaseEntity, IAuditable
  {
     ...
  }

  public enum ContractorSignatureType
  {
    A,
    B,
    V
  }
}

ContractorSignatureType = Contracts.ContractorSignatureType.B,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace @enum
    {
        class temp
        {
           public enum names
            {
                mohitt,
                keval,
               Harshal,
               nikk
            }

        }


        class Program
        {

            static void Main(string[] args)
            {

                Console.WriteLine((int)temp.names.nikk);
                Console.ReadKey();

            }
        }
    }
//by using this you can access the value of enum.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace @enum
{
    class temp
    {
       public enum names
        {
            mohitt,
            keval,
           Harshal,
           nikk
        }

    }
    class Program
    {

        static void Main(string[] args)
        {

            Console.WriteLine(temp.names.nikk);
            Console.ReadKey();

        }
    }
}

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

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