2014-10-02 84 views
0

我試圖讓這個代碼顯示,但它給了我這個錯誤。有人能夠幫助嗎? 只要知道我出錯的地方,提示和建議就足夠了。預期的類,委託,枚舉,接口或結構錯誤

public ObservableCollection<PData> Data = new ObservableCollection<PData>() 
     { 

       new PData() { title = "slice #1", value = 30 }, 
       new PData() { title = "slice #2", value = 60 }, 
       new PData() { title = "slice #3", value = 40 }, 
       new PData() { title = "slice #4", value = 10 }, 

     }; 

      private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
      { 
       pie1.DataSource = Data; 
      } 


    public class PData 
    { 
     public string title { get; set; } 
     public double value { get; set; } 
    } 
+2

你的方法是不是封閉在一個班。除非有更多的代碼沒有顯示。 – 2014-10-02 05:18:41

+0

檢查這個http://stackoverflow.com/questions/4824194/expected-class-delegate-enum-interface-or-struct-error-on-public-static-str – 2014-10-02 05:24:05

回答

1

此錯誤通常意味着你打錯號碼了支架{或者在你的代碼}的地方,或者說(的一部分嗎?)你的代碼是不是一個類中。

在你的情況(如果確實是所有的代碼),以下東西:

public ObservableCollection<PData> Data (...) 

不是class { ... }範圍之內。

如果你有比所示更多的代碼,它類中,尋找一個類或方法定義,有太多的密切括號},這可以解釋爲「一類的年底」 。

簡單例如:

class Stuff{ 

    public void DoStuff(){ 
     ... 
    } 
    } // accidental extra bracket ends the class Stuff too early 

    // Error will show here, since this appears to be outside the class: 
    public void DoSomethingElse(){ 

    } 

} // The inteded end of Stuff to be 
+0

是的,我錯過了班。謝謝您的幫助! – 2014-10-02 07:08:21

0
public class NameList : ObservableCollection<PersonName> 
{ 
    public NameList() : base() 
    { 
     Add(new PersonName("Willa", "Cather")); 
     Add(new PersonName("Isak", "Dinesen")); 
     Add(new PersonName("Victor", "Hugo")); 
     Add(new PersonName("Jules", "Verne")); 
    } 
    } 

    public class PersonName 
    { 
     private string firstName; 
     private string lastName; 

     public PersonName(string first, string last) 
     { 
      this.firstName = first; 
      this.lastName = last; 
     } 

     public string FirstName 
     { 
      get { return firstName; } 
      set { firstName = value; } 
     } 

     public string LastName 
     { 
      get { return lastName; } 
      set { lastName = value; } 
     } 
    } 
相關問題