【问题标题】:Using defines like in C/C++ in C#在 C# 中使用类似于 C/C++ 中的定义
【发布时间】:2012-09-27 09:45:50
【问题描述】:

我正在做一个项目,我需要在设备类型之间进行切换。我想像在 C/C++ 中一样定义它们。我发现这在 C# 中是不可能的。我正在制作一些实验代码来了解它是如何工作的,但我被困在一件事上。我已经在下面的代码中指出了这一点。请看一下 :)。我希望我的代码思路清晰,我应该如何更改它才能使其工作?

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

    private void button1_Click(object sender, EventArgs e)
    {
        productnames pn = new productnames(); 
        string str = textBox1.Text;
        switch (pn) // I am getting an red line under pn; which says: A switch expression or case label must be a bool, char, string, integral, enum or corresponding nullable type
        { 
            case DEVICE1:
                label1.Text = str+"CASE1";
                break;
            case DEVICE2:
                label1.Text = str+"CASE2";
                break;
        }
    }
}
public class productnames
{
    public const string DEVICE1 = "name1";
    public const string DEVICE2 = "name2";
    public const string DEVICE3 = "name3";
    public const string DEVICE4 = "name4";
}

上面的代码不起作用,因为你不能在整个类之间切换。更好的方法是这样的:

private const string DEVICE1 = "dev1";
private const string DEVICE2 = "dev2";

        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text;
            getCommand(str)
        }

private void getCommand(string Dev)
{
            switch (Dev) 
            { 
                case DEVICE1:
                    label1.Text = str+"CASE1";
                    break;
                case DEVICE2:
                    label1.Text = str+"CASE2";
                    break;
            }
    }

这个例子可能很有用:

#define CMD_VERSION 0
#define CMD_TYPE 1

private void getCommandName(CMD)
{
   switch(CMD)
   {
      case CMD_VERSION:
      return ("ver");
      case CMD_TYPE:
      return ("serienummer");
   }
}

【问题讨论】:

  • productnames 不是string。它包含字符串。
  • 我觉得你应该把pn改成“bool、char、string、integral、enum或者对应的可空类型”
  • SIR80SIR100 是什么类型?那是pn 需要的类型。该类型还需要是“bool、char、string、integral、enum 或相应的可为 null 类型”或隐式转换为“bool、char、string、integral、enum 或相应的可为 null 类型”。
  • 抱歉改了,忘了重命名那两个...
  • 解决起来很简单,“productnames”应该是您使用它的方式的枚举。 (或想使用它。)

标签: c# c-preprocessor


【解决方案1】:

让我们发挥创意,添加一个可以在现实生活中更快使用的示例。

using System;

namespace Draft
{
    class Program
    {
        static void Main()
        {
            const string str = "Device2";
            var strParsed = (Devices)Enum.Parse(typeof(Devices), str);

            switch (strParsed) {
                case Devices.Device1:
                    Console.WriteLine("Device 1");
                    break;
                case Devices.Device2:
                    Console.WriteLine("Device 2");
                    break;
                case Devices.Device3:
                    Console.WriteLine("Device 3");
                    break;
                case Devices.Device4:
                    Console.WriteLine("Device 4");
                    break;
            }

            Console.ReadKey();
        }

        public enum Devices {
            Device1,
            Device2,
            Device3,
            Device4
        }
    }
}

[编辑]

似乎我的方向是正确的,只有你的“定义”示例给他们整数(无论如何,他们默认按照你声明它们的顺序)。

但是按照你的例子,它会是这样的:

public enum Devices {
    Device1 = 0,
    Device2 = 1,
    Device3 = 2,
    Device4 = 3
}

【讨论】:

    【解决方案2】:

    您的问题与#define 无关。 switch 语句不能对表达式进行操作,它必须被赋予一个文字值(在引擎盖下 switch 语句被编译为索引跳转表),即它必须是字符串、int 等,而不是对象( )。

    在您的情况下,pn 似乎 pn 应该是字符串或枚举。将您的代码更改为此应该可以工作:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            var pn = productnames.DEVICE3; //obviously pn could be set to anything you like, or passed in to the event handler or function
            string str = textBox1.Text;
            switch (pn) 
            { 
                case productnames.DEVICE1:
                    label1.Text = str+"CASE1";
                    break;
                case productnames.DEVICE1:
                    label1.Text = str+"CASE2";
                    break;
            }
        }
    }
    
    public static class productnames
    {
        public string DEVICE1 = "name1";
        public string DEVICE2 = "name2";
        public string DEVICE3 = "name3";
        public string DEVICE4 = "name4";
    }
    

    【讨论】:

      【解决方案3】:

      我会说,字典会是一种更好(更快)的方式

      var Products = new Dictionary<string, ProductInfo>()
      {
          { "DEVICE1", new ProductInfo("Properties of product 1") },
          { "DEVICE2", new ProductInfo("Properties of product 1") },
          { "DEVICE3", new ProductInfo("Properties of product 1") }
      };
      
      
      var SelectedProduct = Products[textbox1.Text];
      label1.Text = SelectedProduct.Info;  
      

      【讨论】:

        【解决方案4】:

        使用以下代码可以解决此问题:

           public ProductNames
        {
            public readonly static DEVICE1 = new ProductNames("DEVICE1");
            public readonly static DEVICE2 = new ProductNames("DEVICE2");
            public readonly static DEVICE3 = new ProductNames("DEVICE3");
        
            public string Name{get{return _name;}}  
            private readonly _name ;
        
            private ProductNames(string name)
            {
                _name = name;
            }  
        }
        
        // usage
        ProductNames pn = GetProcuctName(/* your logic */);
        switch(pn.Name)
        {
            case ProductNames.DEVICE1.Name:
                // do smth
            case ProductNames.DEVICE2.Name:
                // do smth else
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多