[英]C# instantiating a Class that hold a ListArray into a method
我試圖在我的StoreNames()
方法中實例化ListEx類的實例,以獲取名稱列表並將其存儲在ListEx
類中,以便其他方法可以訪問它們。 我收到一個錯誤,我無法將void轉換為int。 有人可以對此有所了解嗎?
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());
}
////////////////新密碼//////////////////////
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;
}
}
好吧,在這一行:
nm.name.add = huh.Add(Console.ReadLine());
...您正在嘗試使用私有字段,除非StoreNames
位於ListEx中,否則StoreNames
使用。
然后你試圖分配給ListEx
的子字段/屬性...你期望add
什么參考?
您正在嘗試使用huh.Add
當你還沒有表現出什么huh
是
huh.Add
返回的值huh.Add
nm.name.add
,我懷疑Add
有一個void返回類型。 換句話說,它很破碎。 目前還不清楚你要在這里做什么,為什么你需要ListEx
, StoreNames
,或者你為什么要嘗試將Add
方法的返回值賦給字段/屬性。 ListEx
真的只是一個私人領域嗎? 如果是這樣的話,它不會有多大用處。
nm.name.Add( Console.ReadLine());
?
或者(再次閱讀你的帖子:)
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" );
}
心連心
馬里奧
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.