2013-03-31 58 views
-2

我已經使用枚舉類型的數據類型來添加字符串元素在Windows窗體c#中的組合框,但添加元素在其中的錯誤已經發生。 錯誤的是(「識別符預期」) 我的代碼是如何將字符串項添加到c#中的枚舉?

public enum EducationG 
     { 
      ("Bachelor of Arts (B.A)"), 
      ("Bachelor of Arts (Bachelor of Education (B.A. B.Ed)")], 
      ("Bachelor of Arts (Bachelor of Law (B.A.B.L)"), 
      ("Bachelor of Arts (Bachelor of Law (B.A.LLB)"), 
      ("Bachelor of Ayurvedic Medicine and Surgery (B.A.M.S)"), 
      ("Bachelor of Applied Sciences (B.A.S)"), 
      ("Bachelor of Audiology and Speech Language Pathology (B.A.S.L.P)"), 
      ("Bachelor of Architecture (B.Arch)"), 
      ("Bachelor of Business Administration (B.B.A)"), 
      ("Bachelor of Business Administration (Bachelor of Law (B.B.A LL.B)"), 
      ("Bachelor of Business Management (B.B.M)"), 
      ("Bachelor of Business Studies (B.B.S)"), 
      ("Bachelor of Computer Applications (B.C.A)"), 
      ("Bachelor of Communication Journalism (B.C.J)"), 
      ("Bachelor of Computer Science (B.C.S)") 
} 
+2

枚舉不能包含字符串文字。你想做什麼? –

+0

http://msdn.microsoft.com/en-us/library/sbbt4032%28v=vs.80%29.aspx – Kashif

+0

我想使用forloop將字符串元素添加到我的Windows窗體的組合框中 – VJain

回答

0

枚舉不包含字符串。該documentation描述了他們是這樣的:

enum關鍵字來聲明一個枚舉,一個獨特的類型由一組命名的常量稱爲枚舉列表中。每個枚舉類型都有一個基礎類型,可以是除char之外的任何整型。枚舉元素的默認基礎類型是int。默認情況下,第一個枚舉器的值爲0,並且每個連續的枚舉器的值增加1.

也許你正在尋找的是一個字符串數組。

string[] EducationG = { 
    "Bachelor of Arts (B.A)", 
    "Bachelor of Arts (Bachelor of Education (B.A. B.Ed)", 
    .... 
} 

如果你想要做的只是添加一串字符串到組合框,那麼字符串數組是很好的。但是如果你確實需要一個枚舉,那麼你需要聲明一個enum並找到一種方法來映射枚舉值和字符串名稱。 Description屬性將會訣竅。

+0

我用string []替換了關鍵字enum,但它仍然顯示相同的錯誤 – VJain

+0

@user但我沒有建議這樣做。我建議使用'string []'並顯示用數組初始值設定項聲明的語法。當然,我實際上並不知道你想要解決什麼問題。所以,也許你不想要一個字符串數組。你需要慢一點,試着理解你的問題,並正確地學習語言。 –

+0

現在錯誤是「一個獲取或設置訪問器預計 – VJain

1

你不能這樣做,但你可以這樣做:

public enum EducationG 
{ 
    [Description("Bachelor of Arts (B.A)")] 
    BachelorOfArtsBA, 

    ... 
} 

您可以使用枚舉轉換爲字符串:

var enumValue = EducationG.BachelorOfArtsBA; 
var attrs = (DescriptionAttribute[])typeof(EducationG) 
    .GetField(enumValue.ToString()) 
    .GetCustomAttributes(typeof(DescriptionAttribute)); 
var stringValue = attrs[0].Description; 

將字符串轉換回枚舉是一個比較有挑戰性的:

var stringValue = ... 
var enumValue = 
    from f in typeof(EducationG).GetFields(BindingFlags.Static) 
    from d in (DescriptionAttribute[])f.GetCustomAttributes(typeof(DescriptionAttribute)) 
    where d.Description == stringValue 
    select f.GetValue(null); 

在任何情況下,這個解決方案可能更復雜,以至於您的特殊問題需要解決。

+1

一旦你做完了,那又怎麼樣? –

+0

這會起作用,但你也應該向他解釋什麼是枚舉,什麼應該是這種數據的更好方法。 – Kashif

+0

以及如何將描述更改爲枚舉? –

0

請看例子中的鏈接 emun(C#)代碼可能看起來像:

public enum EducationG {SomeName, SomeOtherName}; 

正如你在鏈接也可以默認值和標誌添加到枚舉注意。

0

你也可以使用一個List<string>這將是這樣的:

var educationGrades = new List<string> 
{ 
    "Bachelor of Arts (B.A)", 
    "Bachelor of Arts (Bachelor of Education (B.A. B.Ed)", 
    ... 
    "Bachelor of Computer Science (B.C.S)" 
}; 
0

使用字符串和justcode這樣

private void edulvlcb_SelectedIndexChanged(object sender, EventArgs e) 
     { 

        combobox.Items.Clear(); 
      foreach (string ug in Classname.stringname) 
       combobox.Items.Add(ug); 
    } 

它必須努力

+0

@ user2214972爲什麼你從一個答案中改變了接受問題的答案,這個答案與你提出的問題完全無關,甚至沒有進行遠程編譯?哪些是在你不接受之後很快到達的?沒有'stringname'可以在任何地方看到? –