设置一个枚举等于另一个 [英] Set one enum equal to another

查看:189
本文介绍了设置一个枚举等于另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2枚举在2个不同的对象。我想设置枚举的对象#1等于对象枚举 2

I have 2 enums in 2 different objects. I want to set the enum in object #1 equal to the enum in object #2.

下面是我的对象:

namespace MVC1 {

    public enum MyEnum {
        firstName,
        lastName
      }

   public class Obj1{
        public MyEnum enum1;
    }
   }


     namespace MVC2 {

    public enum MyEnum {
        firstName,
        lastName
      }

    public class Obj2{
        public MyEnum enum1;
      }
    }

我想这样做,但是这不会编译:

I want to do this, but this wont compile:

 MVC1.Obj1 obj1 = new MVC1.Obj1();
 MVC2.Obj2 obj2 = new MVC2.Obj2();
 obj1.enum1 = obj2.enum1; //I know this won't work.

如何设置枚举在OBJ1等于OBJ2枚举?谢谢

How do I set the enum in Obj1 equal to the enum in Obj2? Thanks

推荐答案

假设你让他们相同,就可以转换为/从int:

Assuming that you keep them the same, you can cast to/from int:

obj1.enum1 = (MVC1.MyEnum)((int)obj2.enum1);

这篇关于设置一个枚举等于另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆