简体   繁体   English

如何将枚举类型的值传递给另一个枚举变量?

[英]How to pass an value of type enum to another enum variable?

I'm getting the value of in an enum variable but while assigning it to another variable of type enum its getting 0 all the time...Here's the code i'm using , dbProperties is the object of class DBProperties which has a member of type DBType. 我正在获取enum变量中的值,但是在将其分配给enum类型的另一个变量时,它始终一直为0 ...这是我正在使用的代码,dbProperties是类DBProperties的对象,该类的成员输入DBType。 dbProperties.DBType is always returning 0 even after assigning a value to it...Please help...! 即使为dbProperties.DBType分配了值,它也始终返回0 ...请帮助...!

DBType val = (DBType)cbDataType.SelectedIndex;
cbDataType.SelectedIndex = (int)val;
dbProperties.DBType = val;

So dbProperties.DBType is the name of a variable which is of type DBType? 那么dbProperties.DBType是DBType类型的变量的名称吗?

Not sure why it would be always returning 0 but I don't think it's anything to do with assigning values to enums. 不知道为什么总是返回0,但是我认为这与为枚举赋值无关。

Run this code for example: 例如,运行以下代码:

enum DBType
{
    Int,
    Bool,
    String
}

class DBProperties
{
    public DBType DBType;
}

class Program
{
    static void Main(string[] args)
    {
        DBType d = (DBType)2;
        DBProperties p = new DBProperties();
        p.DBType = d;
        Console.WriteLine((int)p.DBType); //outputs 2
        Console.ReadLine();
    }
}

You need to be much more clear and specific with your question in order for people to help you. 您需要更加清楚明确地回答您的问题,以便人们帮助您。

Are you sure val is not always zero? 您确定val并不总是为零吗? If you always select the first choice in the drop down box, selectedIndex will always be zero. 如果您始终在下拉框中选择第一个选项,则selectedIndex将始终为零。

This works fine for me. 这对我来说很好。 I think you've got a form in which you've got a dropdown combobox with items in it. 我认为您有一个表格,其中有一个下拉式组合框,其中包含项目。 So I replicated that and here is the code. 所以我复制了这些,这里是代码。 The dropdown is populated with items that match (in as far as sequence) those shown in code below (the enum DBType) and shown in the first image attached 下拉列表中填充的项目(按顺序排列)与下面的代码(枚举DBType)中显示的内容以及在所附的第一个图像中显示的内容(按顺序排列)匹配

  enum DBType { Int, Double, Float, Bool, String  }

  class DBProperties
  {
    private DBType dbType;
    public DBType DBType { get { return dbType; } set { dbType = value; } }
  }

  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void cbDataType_SelectedIndexChanged(object sender, EventArgs e)
    {
      DBType val = (DBType)cbDataType.SelectedIndex;
      cbDataType.SelectedIndex = (int)val;
      var dbProperties = new DBProperties();
      dbProperties.DBType = val;
    }
  }

the dbProperties.DBType property is correctly set to the item selected in the dropdown. dbProperties.DBType属性正确设置为下拉菜单中选择的项目。 在此处输入图片说明

On selecting "bool" in the dropdown the property is set to "Bool" as shown in the second image. 在下拉菜单中选择“布尔”后,该属性将设置为“布尔”,如第二幅图像所示。 在此处输入图片说明

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

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