【问题标题】:C# - How can I get access to a List which is inside a method, from another method? [closed]C# - 如何从另一个方法访问方法内的列表? [关闭]
【发布时间】:2020-10-28 08:27:25
【问题描述】:
public partial class Form1 : Form
{   
     public void CreateList()
     {
         List<IRentable> allItems = new List<IRentable>()
        {
            new VideoBook(06841) {Titel = "Community", Rented = true, GenreType = Genre.Comedy, Actor = AllPeople.ElementAt(0)},
            new AudioBook(11585) {Titel = "Deutsch für Dummies", Rented = false, GenreType = Genre.Educational, Author = AllPeople.ElementAt(1)},
            new VideoBook(50862) {Titel = "Interstellar", Rented = false, GenreType = Genre.ScienceFiction, Actor = AllPeople.ElementAt(2)},
            new AudioBook(98065) {Titel = "The Slim Shady LP", Rented = false, GenreType = Genre.Music, Author = AllPeople.ElementAt(3) },
        };
    public Form1()
    {
        InitializeComponent();
        CreateList();
    }

在这里我需要创建一个新方法(我从表单中获取的一个搜索按钮),我可以在其中访问“allitems”元素

    private void searchButton_Click(object sender, EventArgs e)
    {
        
        new List<IRentable> SearchResultItems();
        var a = titleTextBox.Text;

        // here I can't access to allitems

        foreach (var elem in allitems)
        ...
    }
}

【问题讨论】:

  • 你不能。那是一个局部变量,你离开的那一刻它就消失了CreateList
  • allItems定义为属性而不是局部变量?

标签: c# list methods


【解决方案1】:

您可能希望在 class 范围内定义 newItems 列表,而不是在函数范围内,这样您就可以在类中的任何位置访问它。

我建议您阅读有关变量作用域的信息,thisthis 可能是很好的起点。

如果您在 Form1 的类范围内定义 newItems,您的代码如下所示:

public partial class Form1 : Form
{   
    // Notice that this variable is defined outside of functions, but its 
    // value has ben set inside CreateList(), so before CreateList() is
    // called, this variable contains null.
    List<IRentable> allItems;
    
    public void CreateList()
    {
        allItems = new List<IRentable>()
        {
            new VideoBook(06841) {Titel = "Community", Rented = true, GenreType = Genre.Comedy, Actor = AllPeople.ElementAt(0)},
            new AudioBook(11585) {Titel = "Deutsch für Dummies", Rented = false, GenreType = Genre.Educational, Author = AllPeople.ElementAt(1)},
            new VideoBook(50862) {Titel = "Interstellar", Rented = false, GenreType = Genre.ScienceFiction, Actor = AllPeople.ElementAt(2)},
            new AudioBook(98065) {Titel = "The Slim Shady LP", Rented = false, GenreType = Genre.Music, Author = AllPeople.ElementAt(3) },
        };
    }
    
    public Form1()
    {
        InitializeComponent();
        CreateList();
    }

    private void searchButton_Click(object sender, EventArgs e)
    {
        // You can access allItems variable here, because both CreateList() 
        // and searchButton_Click() functions are within the class scope,
        // where the variable was defined.

        foreach (var elem in allItems)
        ...
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-06
    • 2021-01-10
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多