2

我正在做一个项目,我需要在设备类型之间进行切换。我想像在 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");
   }
}
4

4 回答 4

2

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

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
}
于 2012-09-27T10:02:14.103 回答
1

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

在您的情况下,它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";
}
于 2012-09-27T09:55:29.050 回答
1

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

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;  
于 2012-09-27T09:58:29.563 回答
0

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

   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
}
于 2012-09-27T09:55:05.663 回答