我正在开发一个项目,我需要在设备类型之间切换。我想像在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");
}
}
答案 0 :(得分: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
}
答案 1 :(得分:1)
您的问题与#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";
}
答案 2 :(得分: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;
答案 3 :(得分: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
}