C# 枚举文字
我想知道是否有办法用整数声明枚举。或者如果有其他的选择我可以使用 例如:C# 枚举文字,c#,enumeration,C#,Enumeration,我想知道是否有办法用整数声明枚举。或者如果有其他的选择我可以使用 例如: enum panelSizes { 300, 305, 310, 315, ..., 50000} [0] [1] [2] [3] [940] 我需要为每个尺寸分配某种ID,因此当用户输入特定宽度时,我必须能够识别存储在不同数组中的相应切割尺寸 这是我试图避免尝试将excel文件读入我的程序,并进行某种查找以识别某些相关信息 请帮忙 提前感谢由于您的方法不允许使用名称
enum panelSizes { 300, 305, 310, 315, ..., 50000}
[0] [1] [2] [3] [940]
我需要为每个尺寸分配某种ID,因此当用户输入特定宽度时,我必须能够识别存储在不同数组中的相应切割尺寸
这是我试图避免尝试将excel文件读入我的程序,并进行某种查找以识别某些相关信息
请帮忙
提前感谢由于您的方法不允许使用名称,请使用数组:
readonly int[] panelSizes = { 300, 305, 310, 315, ..., 50000};
然后,也许可以添加一个枚举来索引它:
enum panelSizeNames { a300, a305, a310, a315, ... , a50000 } // or better names
得到
int size = panelSizes[panelSizeNames.a315];
字典怎么样
Dictionary<int, int> dic = new Dictionary<int, int>
{
{ 0, 300 },
{ 1, 305 },
{ 2, 310 }
....
};
字典dic=新字典
{
{ 0, 300 },
{ 1, 305 },
{ 2, 310 }
....
};
请注意,如果键是从0到N的索引,那么简单的数组也可以…使用从一开始加载的
字典,其中的id和宽度作为键和值。对我来说,似乎您希望使用算法而不是查找来获得正确的剪切大小。如果您的值是这样线性的,则不需要字典/枚举/数组
int panelSize = 5000;
int index = (panelSize - 300)/5;
反过来说
int index = 940;
int panelSize = (index * 5) + 300;
以下是我所做的:
通过使用结构,我能够将输入数组与填充了大小的数组进行比较,然后将每个匹配大小的位置存储在数组“Identifies”中。现在我可以很容易地编写从相同位置的其他数组返回值的方法。。。(类似于电子表格查找)
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
名称空间PalisadeWorld
{
//结构将每个面板大小的所有“标识”存储在一个数组中
结构ID
{
公共身份;
公共ID(int[]宽度,int行)
{
int[]所有宽度={300305310315320325330,…,5000};
int i,j;
int[]id=新的int[行];
对于(i=0;i
谢谢大家!我甚至在笔记中写下了这一点。对不起,伙计。这很完美如果int
表示的enum
值用作数组索引,我认为最好显式设置枚举值:enum panelSizeNames{a300=0,a305=1,}
。即使现在实际上并不需要它(enum
每个默认值基于0),IMO更好,因为它允许以后轻松更改目标数组中panelSizes值的索引。默认编号是完美的,消除了一个错误源。手动工作越少越好。对于这样的线性值,除非我在这里遗漏了什么,否则不需要字典/枚举来查找。我认为int[]因为我比较的是来自DataGrid的用户输入和int[]的值。然后,该位置将允许我从不同的数组中获取所需的相应值,因为它们位于同一位置。但谢谢你,我热衷于尽我所能学习。我对C非常陌生。事实上,我有一个电子表格,其中包含每个面板大小的各自切割大小、孔数,具体取决于间隙大小和时间大小是否适合安装。因此我将使用相同的参考(位置)获取所有信息,但您的电子表格是线性的?300 | 0305 | 1310 | 2等5000 | 940?对吗?使我提供的算法返回与您的电子表格相同的值。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PalisadeWorld
{
//struct to store all the 'identities' of each panel size in an array
struct ID
{
public int[] Identities;
public ID(int[] widths, int rows)
{
int[] allWidths = { 300, 305, 310, 315, 320, 325, 330, ..., 5000 };
int i,j;
int[] Ids = new int[rows];
for (i = 0; i < rows; i++)
{
for (j = 0; j < 941; j++)
{
if (widths[i] == allWidths[j])
{
Ids[i] = j;
break;
}
}
}
this.Identities = Ids;
}
public override string ToString()
{
string data = String.Format("{0}", this.Identities);
return data;
}
}
class LookUpSheet
{
//retrieve calculated widths and number of panels from NewOrder.cs
public int[] lookUp_Widths {get; set;}
public int lookUp_Rows { get; set; }
//Method returning number of pales
public int[] GetNumPales1()
{
int[] all_numPales = { 2, 2, 2, 2, 2, 2, 2, 2, 2, ..."goes on till [941]"...};
int[] numPales = new int[lookUp_Rows];
ID select = new ID(lookUp_Widths, lookUp_Rows);
for (int i = 0; i < lookUp_Rows; i++)
{
numPales[i] = all_numPales[select.Identities[i]];
}
return numPales;
}
//Method returning block sizes (mm)
public int[] GetBlocks1()
{
int[] all_blocks = { 56, 59, 61, 64, 66, 69, 71, 74, "goes on till [941]"...};
int[] blocks = new int[lookUp_Rows];
ID select = new ID(lookUp_Widths, lookUp_Rows);
for (int i = 0; i < lookUp_Rows; i++)
{
blocks[i] = all_blocks[select.Identities[i]];
}
return blocks;
}