【发布时间】: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?此外,您不会将任何值放入您从GetList或MyClass返回的myList。 -
前 2 块代码是我的工作。另外两个来自here