2013-02-27 117 views
2

我想知道是否有某種方式我可以用整數聲明一個枚舉。或者如果有其他的選擇我可以使用。枚舉文字

例如:

enum panelSizes { 300, 305, 310, 315, ..., 50000} 
        [0] [1] [2] [3]  [940] 

我需要某種ID的分配給每個的大小,所以當用戶輸入一個特定的寬度,我必須能夠識別相應的切截尺寸女巫被存儲在不同陣列。

這是我試圖避免嘗試讀取我的程序中的excel文件,並做某種查找來識別某些相關信息。

請幫

在此先感謝

+0

有了這樣的線性值,不需要字典/枚舉來查找,除非我在這裏丟失了一些東西。 – Evelie 2013-02-27 13:36:20

回答

3

因爲你的做法不允許的名字,無論如何,使用數組:

readonly int[] panelSizes = { 300, 305, 310, 315, ..., 50000}; 

,然後,也許,一個枚舉添加到索引它:

enum panelSizeNames { a300, a305, a310, a315, ... , a50000 } // or better names 

得到

int size = panelSizes[panelSizeNames.a315]; 
+0

我甚至在筆記中寫下了這個。對不起。隊友。這是完美的 – 2013-02-27 13:30:06

+0

如果'enum'值的int形式用作數組索引,我認爲最好明確設置枚舉值:'enum panelSizeNames {a300 = 0,a305 = 1,...}' 。即使現在實際上並不需要('enum'默認爲0),但它更好的IMO,因爲它允許稍後輕鬆地更改目標數組中的panelSizes值的索引。 – ken2k 2013-02-27 13:36:19

+0

默認編號是完美的,並消除了一個錯誤來源。手工工作越少越好。 – 2013-02-27 13:37:58

1

字典怎麼樣?

 Dictionary<int, int> dic = new Dictionary<int, int> 
     { 
      { 0, 300 }, 
      { 1, 305 }, 
      { 2, 310 } 
      .... 
     }; 

請注意,如果關鍵是從0到N的索引,一個簡單的數組會太行...

+0

我認爲int []會少一點努力,因爲我將來自DataGrid的用戶輸入與int []的值進行比較。那麼這個職位將允許我從不同的陣列中獲得我需要的相應值,因爲他們處於相同的位置。但是,謝謝你,我熱衷於盡我所能學習。我對C#非常陌生# – 2013-02-27 13:44:11

1

使用Dictionary<int,int>您在開始使用id和寬度加載在它作爲關鍵和價值。

2

對我來說,你似乎想要使用算法而不是查找來獲得正確的cutize。如果你的值是線性的,就不需要字典/枚舉/數組。

int panelSize = 5000; 
    int index = (panelSize - 300)/5; 

和周圍

int index = 940; 
    int panelSize = (index * 5) + 300; 
+0

事情是,我實際上有一個電子表格,每個面板的各自的切割尺寸,孔數量取決於間隙尺寸以及尺寸是否適合安裝。所以我將使用相同的參考(位置)來獲取所有信息 – 2013-02-27 14:29:01

+0

但是您的電子表格是線性的嗎? 300 | 0,305 | 1,310 | 2等等5000 | 940?對?使我提供的算法返回與您的電子表格相同的值。 – Evelie 2013-02-27 14:39:36

0

這裏的另一種方式是我所做的:

使用一個結構,我能夠輸入數組比較填充的大小,然後存儲陣列每個匹配大小在陣列「標識」中的位置。現在,我可以輕鬆地編寫方法在相同的位置返回來自其他數組值...(類似電子表格查找)

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; 
    } 

謝謝大家!