2017-07-31 109 views
0
上拋出錯誤

我正在運行VS 2015 v14.0.25431.01更新3.當我嘗試編譯以下代碼時,出現問題模塊中第1行的錯誤「CS1513} expected」 。怎麼了?VS 2015編譯器在struct

<!-- Build a structure of States. --> 
@using System.Collections; 
@{ 
    struct structState 
    { 
     private string s_abbrev; 
     public string abbrev 
     { 
      get 
      { 
       return s_abbrev; 
      } 
      set 
      { 
       s_abbrev = value; 
      } 
     } 

     private string s_name; 
     public string name 
     { 
      get 
      { 
       return s_name; 
      } 
      set 
      { 
       s_name = name; 
      } 
     } 

     public structState(string a_abbrev, string a_name) 
     { 
      s_abbrev = a_abbrev; 
      s_name = a_name; 
     } 
    } 

    structState[] dumState = new structState[50]; 

} 
+0

定義在剃刀視圖中嗎? – Andez

+0

像這樣的類型不應該是一個結構體,它應該是一個類,因爲它是可變的。它也應該幾乎可以肯定地在它自己的.cs文件中定義,而不是在標記中內聯。 – Servy

+0

是的,Andez是在剃刀視圖中定義的。 – Mark

回答

0

請在@functions指令中定義您的結構來解決問題。

@{ 
    structState[] dumState = new structState[50]; 
} 


@functions 
{ 

    struct structState 
    { 
     private string s_abbrev; 
     public string abbrev 
     { 
      get 
      { 
       return s_abbrev; 
      } 
      set 
      { 
       s_abbrev = value; 
      } 
     } 

     private string s_name; 
     public string name 
     { 
      get 
      { 
       return s_name; 
      } 
      set 
      { 
       s_name = name; 
      } 
     } 

     public structState(string a_abbrev, string a_name) 
     { 
      s_abbrev = a_abbrev; 
      s_name = a_name; 
     } 
    } 

} 
+0

工作。現在,當我嘗試創建一個結構數組時,它無法識別我定義的數組名(數組名不出現在Intellisense下):structState [] arrayState = new structState [50]; – Mark

+0

我在VS 2017上沒有發生智能感知問題。記住,你不能在你定義的視圖之外使用這個結構。如果你想共享,你必須在代碼中定義它(不在視圖中)。 –

+0

沒關係。我需要將我的結構設置爲public - DOH!現在正在工作。發送幫助! – Mark

相關問題