【问题标题】:How to read a List from one class in another如何从另一个类中读取列表
【发布时间】:2017-03-04 04:38:11
【问题描述】:

好的,所以在我正在处理的控制台应用程序中,我在 Class01 中有一个列表 (myList)

class Class01
{
    public List<string> myList = new List<string>();

    public void _addsList()
    {
        myList.Add("0001Test01");
        myList.Add("0002Test02");
        myList.Add("0003Test03");
        myList.Add("0004Test04");

        for (int i = 0; i < myList.Count; i++)
        {
            Console.WriteLine(myList[i] + ",");
        }
    }
}

我需要在 Class02 中阅读该列表

class Class02
{
    public void _callList()
    {
        var class02 = new Class01();
        string wits2;

        List<string> buffer = new List<string>();
        for (int i = 0; i < class02.myList.Count; i++)
        {
            wits2 = class02.myList[i].Substring(0, 4);
            Console.WriteLine(class02.myList[i]);
        }

        Console.ReadKey();

    }
}

这个程序的输出应该把这个写到控制台:

0001Test01,

0002Test02,

0003Test03,

0004Test04,

0001

0002

0003

0004

现在我已经看到 GetList 曾经这样做

public class MyClass {

    private List<string> myList = new List<string>();

    public List<string> GetList()
    {
        return myList;
    }
 } 


public class CallingClass {

    MyClass myClass = new MyClass();

    public void GetList()
    {
        List<string> calledList = myClass.GetList();
        ///More code here...
    }
}

但我这辈子都无法让它发挥作用。我不知道我是否缺少名称空间或什么。我什至不知道 GetList 是否适用于控制台应用程序。

所以我非常感谢您的帮助。

谢谢-

【问题讨论】:

  • 什么是can not get this to work?此外,您不会将任何值放入您从GetListMyClass 返回的myList
  • 前 2 块代码是我的工作。另外两个来自here

标签: c# console-application


【解决方案1】:

虽然从你的问题中没有完全清楚什么是行不通的。但我怀疑您需要将值放入列表中。如果您的意思是它没有在控制台中显示任何内容,请尝试以下操作:

 var class02 = new Class01();
 string wits2;

 class02._addsList();

编辑:阅读您的评论后,我认为这将解决它:

for (int i = 0; i < class02.myList.Count; i++)
{
    wits2 = class02.myList[i].Substring(0, 4);
    Console.WriteLine(class02.myList[i]);
    buffer.Add(wits2);//Add it to list declared in this function
}

注意:如果您的意思是在类对象上调用_addsList 时得到输出,请确保在使用Class01 的对象之前调用了此函数一次。从Class02 的第二个代码块开始,您没有在Class01 的对象上调用_addsList 函数。

【讨论】:

  • (自我注意:更好地措辞这些)我的问题是 Class02 无法从 Class01 读取列表 Class01 写入完整列表 Class02 假设读取 Class01 中的列表并仅存储前 4它自己的列表中的数字。所以“0001Test1”在新列表中保存为“0001”
  • 您是否遇到任何异常等情况?两个类是否在同一个项目中定义。尝试将class01 声明为internal
【解决方案2】:

在你的 Class02 上:

        var class02 = new Class01();
        class02._addsList(); //Add this on your declaration
        string wits2;

然后更改您在 Console.WriteLine 上传递的变量:

List<string> buffer = new List<string>();
        for (int i = 0; i < class02.myList.Count; i++)
        {
            wits2 = class02.myList[i].Substring(0, 4);
            //Console.WriteLine(class02.myList[i]); //Remove this
            Console.WriteLine(wits2);  //Use wits2 instead since this is what you get on your Substring
        }

【讨论】:

    【解决方案3】:

    我认为您缺少的最重要的事情是您的课程没有被标记为公开

    所以请在类声明之前添加 public 如下

        public class Class02
        {
        ......
    

    还有一件事,为了获得您需要的输出,在您的 class02 中,您将子字符串分配给 'wits' 变量但不打印它。相反,您将值从列表传递到 Console.Writeline。

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      感谢 TheVillageIdiot。我现在开始工作了。所以谢谢。我的代码现在看起来像这样:

      public class Class01
      {
          public List<string> myList = new List<string>();
      
          public void _addsList()
          {
              myList.Add("0001Test01");
              myList.Add("0002Test02");
              myList.Add("0003Test03");
              myList.Add("0004Test04");            
          }
      }
      
      class Class02
      {
          public void _callList()
          {
              var class01 = new Class01();
              class01._addsList(); //<--- Had to add this line
      
              for (int i = 0; i < class01.myList.Count; i++)
              {
                  Console.WriteLine(class01.myList[i] + ",");
              }
      
              string wits2;
      
              List<string> buffer = new List<string>();
              for (int i = 0; i < class01.myList.Count; i++)
              {
                  wits2 = class01.myList[i].Substring(0, 4);
                  Console.WriteLine(wits2);
              }
      
              Console.ReadKey();
      
          }
      }
      

      对于不了解问题的硬汉,Class02 看不到 Class01 中的 List。

      感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 2020-08-05
        相关资源
        最近更新 更多