[英]C# instantiating a Class that hold a ListArray into a method
I am trying to instantiate an instance of the ListEx class in my StoreNames()
method in order to get a list of names and store it in the class ListEx
so other methods can access them. 我试图在我的
StoreNames()
方法中实例化ListEx类的实例,以获取名称列表并将其存储在ListEx
类中,以便其他方法可以访问它们。 I recieve an error that I cannot convert a void to an int. 我收到一个错误,我无法将void转换为int。 Can someone shed a little light on this?
有人可以对此有所了解吗?
public class ListEx
{
List<string> name = new List<string>();
}
string StoreNames()
{
ListEx nm = new ListEx();
List<string> tmpName = new List<string>();
nm.name.add = tmpName.Add(Console.ReadLine());
}
////////////////new Code////////////////////// ////////////////新密码//////////////////////
class Program
{
public static List<string> localList = new List<string>();
static void Main(string[] args)
{
List<string> a = new List<string>();
SortName(a);
Console.Read();
}
public static List<string> StoreName(List<string> aString)
{
aString = new List<string>();
localList.Add("c");
localList.Add("a");
localList.Add("b");
localList.Add("d");
foreach (string s in localList)
{
Console.WriteLine(s);
}
Console.Read();
return aString;
}
public static List<string> SortName(List<string> aString)
{
StoreName(aString);
localList.Sort();
foreach (string s in localList)
{
Console.WriteLine(s);
}
return localList;
}
}
Well, in this line: 好吧,在这一行:
nm.name.add = huh.Add(Console.ReadLine());
... you're trying to use a private field, which won't work unless StoreNames
is within ListEx. ...您正在尝试使用私有字段,除非
StoreNames
位于ListEx中,否则StoreNames
使用。
You're then trying to assign to a subfield/property of ListEx
... what are you expecting add
to refer to? 然后你试图分配给
ListEx
的子字段/属性...你期望add
什么参考?
You're trying to use huh.Add
when you haven't shown what huh
is 您正在尝试使用
huh.Add
当你还没有表现出什么huh
是
huh.Add
to nm.name.add
, and I suspect that Add
has a void return type. huh.Add
返回的值huh.Add
nm.name.add
,我怀疑Add
有一个void返回类型。 In other words, it's pretty broken. 换句话说,它很破碎。 It's not really clear what you're trying to do here, why you need
ListEx
, where StoreNames
is, or why you're trying to assign the return value of the Add
method to a field/property. 目前还不清楚你要在这里做什么,为什么你需要
ListEx
, StoreNames
,或者你为什么要尝试将Add
方法的返回值赋给字段/属性。 Does ListEx
really just consist of a private field? ListEx
真的只是一个私人领域吗? It's not going to be much use if so. 如果是这样的话,它不会有多大用处。
nm.name.Add( Console.ReadLine());
? ?
Or (after reading your post again:) 或者(再次阅读你的帖子:)
public class ListEx
{
public List<string> name = new List<string>();
}
void StoreName()
{
ListEx nm = new ListEx();
List<string> localList = new List<string>();
localList.Add ( "whatever" );
nm.name = localList;
}
void StoreNameShort()
{
ListEx nm = new ListEx();
nm.name.Add( "whatever" );
}
hth 心连心
Mario 马里奥
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.