i got stuck while making a method, which i want to return List which is declares inside the method, i just cant get it to work;
heres what im currently doing:
class T{public String s; public int i;}
class Main
{
public List
{
List
temp.AddRange(new T());
temp[0].s = "hello";
temp[0].i = 1234;
return temp
}
}
Error 1 The best overloaded method match for 'System.Collections.Generic.List
Error 2 Argument '1': cannot convert from 'WindowsFormsApplication1.T' to 'System.Collections.Generic.IEnumerable
obviously i dont need to use list in this example, however i have a project which does i just want to know how to make the method work
many thanks
dobbyPosted Jul 8, 2008, 5:08 PM
the issues was that the method was unable to access the struct class as it defaults to private. once i change both the main class and the struct to public all was well.
SriPosted Jul 8, 2008, 2:05 PM
public
List<T> makeList(){
List<T> temp = new List<T>();temp.Add(
new T());temp[0].s =
"hello";temp[0].i = 1234;
return temp;}
Try this instead. I have used AddRange instead off Add since you are only adding one object and the return does not have to specify anything about the class. The variable declaration automatically lets the compiler know the signature of the returning variable and errors if the variable returning is something other than the one expected in the method declaration.
AlanPosted Jul 8, 2008, 2:04 PM
This should work:
public List makeList() temp = new List();
{
List
temp.Add(new T());
temp[0].s = "hello";
temp[0].i = 1234;
return temp;
}