枚举文字
本文关键字:文字 枚举 | 更新日期: 2023-09-27 18:01:35
我想知道是否有某种方法可以声明一个整数枚举。或者,如果有其他选择,我可以使用。
例如:enum panelSizes { 300, 305, 310, 315, ..., 50000}
[0] [1] [2] [3] [940]
我需要为每个尺寸分配某种类型的ID,所以当用户输入特定的宽度时,我必须能够识别存储在不同数组中的各自的cutSize。
这是我的尝试,以避免试图读取一个excel文件到我的程序,并做一些查找,以确定某些相关信息。
请帮
Thanks in advance
由于您的方法不允许命名,因此使用数组:
readonly int[] panelSizes = { 300, 305, 310, 315, ..., 50000};
,然后,可能,添加一个enum来索引它:
enum panelSizeNames { a300, a305, a310, a315, ... , a50000 } // or better names
获取
int size = panelSizes[panelSizeNames.a315];
对我来说,似乎你想要使用算法而不是查找来获得正确的裁剪尺寸。如果你的值是线性的,就不需要字典/枚举/数组了。
int panelSize = 5000;
int index = (panelSize - 300)/5;
和
int index = 940;
int panelSize = (index * 5) + 300;
字典怎么样?
Dictionary<int, int> dic = new Dictionary<int, int>
{
{ 0, 300 },
{ 1, 305 },
{ 2, 310 }
....
};
请注意,如果键是一个从0到N的索引,一个简单的数组也可以…
使用您在开始时加载的Dictionary<int,int>
,其中的id和宽度作为键和值
我是这样做的:
使用结构体,我能够比较输入数组与填充大小的数组,然后将每个匹配大小的位置存储在数组"身份"中。现在我可以很容易地编写从相同位置的其他数组返回值的方法…(类似于电子表格查找)
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;
}
谢谢大家!