在第 1 类中制作 2d 列表,在第 2 类中填写并在第 3 类中使用它

Make a 2d list in class 1, fill it in class 2 and use it in class 3

提问人:Daan 提问时间:2/2/2017 更新时间:2/4/2017 访问量:245

问:

正如标题所暗示的那样,我已经在第 1 类中制作/初始化/任何 2D 列表,我在第 2 类中用东西填充它,并尝试在第 3 类中使用其内容。

但是,在类 2 中填写列表(我已确认它已被填充)后,当调用要使用类的类 3 中的方法时,它只是空的,我不知道为什么。

第 1 类

class class1
{
    public List<List<String>> playerInfo = new List<List<String>>();
    //it's a 2D list but as far as I know that shouldn't be a problem

第 2 类

public sealed partial class class2: Page
{
    class1 host = new class1();

    private void joinButton_Click(object sender, RoutedEventArgs e)
    {
        if (host.playerInfo.Count() <= 4)
        {

            //fill list             

到目前为止,它似乎还可以。如果我做一个计数,它显示它包含 2 个元素,很酷。

第 3 类

public sealed partial class MainPage : Page
{

    GameHost host = new GameHost();

    public void Init()
    {

        if (host.playerInfo.Count() >= 2)
        {
            //Do stuff
         }
    }
}

然而,这里的列表只是空的。Count 只返回 0。

这是什么?

如果我在这里的例子不是很清楚,请告诉我,我还不是很擅长这个 Stack Overflow 的东西。

c# UWP的 嵌套列表

评论

1赞 elirandav 2/2/2017
您能否在您的问题描述中添加如何将 Class1 instasce 转移到 MainPage?

答:

0赞 elirandav 2/2/2017 #1

无论如何,其常见根本原因是覆盖列表引用。 请参阅下面的简单示例:

static void Main()
{
            List<int> mainList = new List<int>();
            UpdateList(mainList);
            Console.WriteLine(mainList.Count); // prints 0
}

private static void UpdateList(List<int> numbers)
{
            numbers = new List<int>();
            numbers.Add(1);
            numbers.Add(2);
            Console.WriteLine(numbers.Count); // prints 2
}

列表引用在 UpfdateList 函数中传输。使用 new 关键字执行 UpfdateList 中的第一行后,您将失去对 main 列表的引用,现在引用是在 UpfdateList 中创建的新列表。

若要修复此问题,请不要覆盖列表引用:

private static void UpdateList(List<int> numbers)
{
                //numbers = new List<int>(); remove that
                numbers.Add(1);
                numbers.Add(2);
                Console.WriteLine(numbers.Count); // prints 2. 'numbers' still holds the reference the the list from the main 
}

此外,请确保在 Class2 和 Class3 中使用相同的 Class1 实例。

0赞 Mong Zhu 2/3/2017 #2

问题在于您在类中创建了一个新实例。此实例在列表中没有值。您可以简单地将实例作为参数传递到方法中:MainPageInit

public sealed partial class MainPage : Page
{    
    public void Init(GameHost host)
    {    
        if (host.playerInfo.Count() >= 2)
        {
            //Do stuff
        }
    }
}

如果要在整个类中使用该实例,也可以通过构造函数传递它并初始化字段,如下所示:GameHostMainPage

public sealed partial class MainPage : Page
{
    GameHost host;

    public MainPage (GameHost _host)
    {    
        host = _host;
    }
}

现在,它可以使用您之前拥有的所有初始化值的类。

public sealed partial class MainPage : Page
{
    GameHost host;

    public MainPage (GameHost _host)
    {    
        host = _host;
    }

    public void Init(GameHost host)
    {    
        if (host.playerInfo.Count() >= 2) // now it should have the desired items
        {
            //Do stuff
        }
    }
}
0赞 AVK 2/3/2017 #3

这是我的做法。

初始化 2D 列表App.xaml.cs on App Launch

private static List<List<String>> _playerInfo;
public static List<List<String>> PlayerInfo
{
    get
    {
        return (_playerInfo == null) ? new List<List<string>>() : _playerInfo;
    }
    set { _playerInfo = value; }
}

现在不需要第 1 类。

class2将是

public sealed partial class class2 : Page
{
    private void joinButton_Click(object sender, RoutedEventArgs e)
    {
        if (App.PlayerInfo.Count <= 4)
        {
            // Fill your List Here
        }
    }
}

并且将MainPage

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        if (App.PlayerInfo.Count >=2)
        {
            // Do Stuff.
        }
    }
}