2012-01-17 22 views
0

我對編程很陌生,所以這可能是一個愚蠢的問題,但我想知道是否可以在方法中放置一個List<T>並訪問它並從該方法之外進行編輯?你可以調用List(T)放置在另一個方法中,如何啓動List(T)?

我應該如何實例化List<T>類以便能夠從另一個類訪問和編輯它?也許我甚至不能以這種方式實例化列表。

這裏是我的包含列表的發起代碼:

public void MyMethod() 
{ 
    List<Customer> newCustomer = new List<Customer> 
    { 
     new Customer 
     { 
      Name="A",     //Name and Telephone are properties. 
      Telephone="02-333444" 
     }, 
     new Customer 
     { 
      Name="B", 
      Telephone="03-444555" 
     }, 
     new Customer 
     { 
      Name="D", 
      Telephone="03-444555" 
     }, 
    }; 
} 

如果我想進入,然後從列表從這個方法怎麼辦之外添加或刪除的項目?我已經嘗試了幾件事情,真的可以使用一些建議?例如;我想在代碼中的其他地方使用newCustomer.Add(...);

任何建議將有所幫助。

回答

8

可以返回你已經從你的方法創建的列表:

public List<Customer> MyMethod() 
{ 

    List<Customer> newCustomer = new List<Customer> 
    { 
     new Customer 
     { 
     Name="A",     //Name and Telephone are properties. 
     Telephone="02-333444" 
     }, 

     new Customer 
     { 
     Name="B", 
     Telephone="03-444555" 
     }, 
     new Customer 
     {  

     Name="D", 
     Telephone="03-444555" 
     }, 

    }; 

    return newCustomer; 
} 

請注意,我改變了你的返回類型從無效到List<Customer>然後在函數結束時返回您生成的列表(return newCustomer)。

當你從別的地方調用你的方法,你就能獲取返回的列表是這樣的:

List<Customer> customerList = MyMethod(); 

或者類之外:

MyClass instanceOfClass = new MyClass(); 
List<Customer> customerList = instanceOfClass .MyMethod(); 

這是編程的基礎知識,我認爲在深入研究發展之前,你應該先閱讀一些核心原則。

1

您需要允許其他代碼才能訪問對該對象的引用。您可以將其作爲類中的屬性公開,也可以從方法中返回。由後者選擇去使用你上面的例子,你會改變你的方法看起來是這樣的:

public List<Customer> MyMethod() 
{ 
    List<Customer> newCustomer = new List<Customer> 
    { 
      new Customer 
      { 
      Name="A",     //Name and Telephone are properties. 
      Telephone="02-333444" 
      }, 

      new Customer 
      { 
      Name="B", 
      Telephone="03-444555" 
      }, 
      new Customer 
      { 
      Name="D", 
      Telephone="03-444555" 
      }, 
    }; 

    return newCustomer; 
} 

然後你就可以使用訪問:

// ... some other code ... 

// SomeClass contains the MyMethod() 
var someClass = new SomeClass(); 

List<Customer> customers = someClass.MyMethod(); 
customers.Add(new Customer()); 
+0

感謝您的建議! – user1153537 2012-01-17 09:51:31

1

您已經返回從列表方法:

public List<Customer> MyMethod() { 
    List<Customer> newCustomer = new List<Customer> 
    { 
     new Customer 
     { 
      Name="A",     //Name and Telephone are properties. 
      Telephone="02-333444" 
     }, 
     new Customer 
     { 
      Name="B", 
      Telephone="03-444555" 
     }, 
     new Customer 
     { 
      Name="D", 
      Telephone="03-444555" 
     }, 
    }; 
    return newCustomer; 
} 
從這個方法以外

現在,你可以做以下存取權限列表:

List<Customer> customers = MyMethod(); 
+0

謝謝你的所有建議!會嘗試他們! – user1153537 2012-01-17 09:46:59

1

你需要存儲一些列表的方法(通常在你的類的字段)外

class MyClass 
{ 
    private List<Customer> _customers = CreateCustomerList(); 

    public List<Customer> CreateCustomerList() 
    { 

     List<Customer> newCustomer = new List<Customer> 
     { 
      new Customer 
      { 
      Name="A",     //Name and Telephone are properties. 
      Telephone="02-333444" 
      }, 

      new Customer 
      { 
      Name="B", 
      Telephone="03-444555" 
      }, 
      new Customer 
      { 
      Name="D", 
      Telephone="03-444555" 
      }, 

     }; 
    } 

    public void AddCustomer(Customer customer) 
    { 
     _customers.Add(customer); 
    } 

} 
2

@ThePower的答案是正確的。
反正你可以使用不同的解決方案:定義你的類內部的變量,因此使得它從類中的所有方法訪問:

public class Foo 
{ 
    private List<Customer> newCustomer = new List<Customer>(); 
    public void MyMethod() 
    { 
     newCustomer.Add(...); 
    } 
} 

public class Foo 
{ 
    private List<Customer> newCustomer = null; 
    public Foo() 
    { 
     newCustomer = new List<Customer> 
     { 
        new Customer 
        { 
         Name="A",     //Name and Telephone are properties. 
         Telephone="02-333444" 
        }, 
        new Customer 
        { 
         Name="B", 
         Telephone="03-444555" 
        }, 
        new Customer 
        { 
         Name="D", 
         Telephone="03-444555" 
        }, 
      }; 
    } 
    public void MyMethod() 
    { 
     newCustomer.Add(...); 
    } 
} 
+0

好點,我想這一切都歸結爲功能的範圍和目的。 – ThePower 2012-01-17 09:47:41

1

你可以返回新客戶名單從方法:

public List<Customer> MyMethod() 
{ 

    List<Customer> newCustomer = new List<Customer> 

    { 
     new Customer 
     { 
      Name="A",     //Name and Telephone are properties. 
      Telephone="02-333444" 
     }, 

     new Customer 
     { 
      Name="B", 
      Telephone="03-444555" 
     }, 
     new Customer 
     { 
      Name="D", 
      Telephone="03-444555" 
     } 
    }; 

    return newCustomer; 
} 

然後調用該方法:

List<Customer> myList = MyMethod(); 

然後,您可以在父對象中訪問它,並且可以將它作爲參數傳遞給其他方法。如果您希望Google瞭解有關該主題的更多信息,則代碼中可用,創建和銷燬的變量的一般主題稱爲「範圍」。

0

不是直接的,這是有點封裝的點。

你可以,但:

  1. 從方法中設置一個變量的方法之外。
  2. 返回列表。
  3. 將列表傳遞給您的方法調用的另一個方法。
  4. 讓委託人獲取列表。
相關問題