2016-08-11 67 views
0

我下面的代碼片段:如何在同一個類和文件中添加列表對象?

public class Database { 
    // fake class 
    public class databaseEntry 
    { 
     public Notification myNotification { get; set; } 
     private string myStatus { get; private set; } 
    } 

    // fake notifications 
    Notification notification1 = new Notification(); 
    Notification notification2 = new Notification(); 

    // fake database of type databaseEntry in List data structure 
    List<databaseEntry> myDatabase = new List<databaseEntry>(); 

    // add fake data to fake database 
    myDatabase.Add(new databaseEntry() { myNotification = notification1, myStatus = "approved"}); 
    myDatabase.Add(new databaseEntry() { myNotification = notification2, myStatus = "pending"}); 

    } 

目前的問題是CS0103:命名爲「MYDATABASE」並不在當前的背景下存在的代碼,最後兩行。

很明顯,我安裝了「myDatabase」,所以我不明白爲什麼Visual Studio會說「myDatabase」不存在。

我rougly隨後例如在 https://msdn.microsoft.com/en-us/library/3wcytfd1(v=vs.110).aspx

+1

要看你有這樣的代碼行'myDatabase.Add('? – Rahul

+0

我們需要更多的背景。如果這些線都是在同一個方法那麼就不可能有這個錯誤,請解釋一下,你聲明myDatabase變量和你使用它的地方 – Steve

+0

你能否進一步解釋Rahul?我不知道你的意思 –

回答

0

提供使用構造函數
需要兩到現在流行的列表上的每個
你沒有myStaus和通知有問題
一類具有自身的名單是奇數

public class Database { 
// fake class 
public class databaseEntry 
{ 
    public Notification myNotification { get; set; } 
    private string myStatus { get; private set; } 
} 

// fake notifications 
Notification notification1 = new Notification(); 
Notification notification2 = new Notification(); 

// fake database of type databaseEntry in List data structure 
List<databaseEntry> myDatabase = new List<databaseEntry>(); 

public DataBase() 
{ 
    // need to do it in the ctor 
    // add fake data to fake database 
    myDatabase.Add(new databaseEntry(true) { myNotification = notification1, myStatus = "approved"}); 
    myDatabase.Add(new databaseEntry(false) { myNotification = notification2, myStatus = "pending"}); 
} 

public DataBase(bool skip) 
{ 
} 

} 
0

你必須在構造這些databaseEntry項目添加到您的List<T>。以這種方式在類中添加它們在C#中是不可能的。你可以創建一個類型的實例,但不能調用它的方法。

public class Database { 
    //fake class 
    public class databaseEntry 
    { 
     public Notification myNotification { get; set; } 
     private string myStatus { get; private set; } 
    } 

    //constructor of your DataBase class 
    public DataBase(){ 
     // add fake data to fake database 
     myDatabase.Add(new databaseEntry() { myNotification = notification1, myStatus = "approved"}); 
     myDatabase.Add(new databaseEntry() { myNotification = notification2, myStatus = "pending"}); 

    } 

    // fake notifications 
    Notification notification1 = new Notification(); 
    Notification notification2 = new Notification(); 

    // fake database of type databaseEntry in List data structure 
    List<databaseEntry> myDatabase = new List<databaseEntry>(); 

} 

另一種方式是一個列表初始化。您可以避免使用構造函數,並直接在List<T>定義中添加項目。此初始化程序不起作用,除了調用Add,但您可以直接在字段定義中使用它。

List<databaseEntry> myDatabase = new List<databaseEntry>(){ 
    new databaseEntry() { myNotification = notification1, myStatus = "approved"}, 
    new databaseEntry() { myNotification = notification2, myStatus = "pending"}, 
}; 
相關問題