Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a method like
public List<patientlogin> Patientlog(string Username, string Password)
{

}

and i want to return 2 list from this method.I used something like tuples and reference but no use
please give some suggestions to fix this issue.

i want to return like
public List<patientlogin> Patientlog(string Username, string Password)
{
List<patientlogin>obj1=new List<patientlogin>();
List<patientlogin>obj2=new list<patientlogin>();

return obj1,obj2;

}

Thanks in Advance.
Posted
Updated 17-Mar-16 10:12am
v2

Hi Guys,

It was great chance to present my solution, if this is not appropriate please let me know your comments.

C#
public class StudentService
{
  public List<student> GetAllStudent(out List<course> ListOfCourse)
  {
    var ListAllStudent = dbContext.Students.ToList();
    var ListOfCourse = dbContext.Students.Courses.ToList();

    return ListAllStudent; 
  }
}

public class StudentController
{
  public ActionResult GetAll()
  {
    StudentService objStudentService = new StudentService(); 
    List<course> CourseList = null;
    List<student> ListStudents = objStudentService.GetAllStudent(out CourseList);
    // This is pointless. If you want a different name, just use it! 
    var ListCourses = CourseList;

  }
}


So in this way you'll get ListStudents and CourseList.

Edit MTH: a bit of cleanup and typo fixing.
 
Share this answer
 
v2
Comments
Matt T Heffron 17-Mar-16 17:41pm    
This question is 2 years old, but since you used out parameters (for the most part correctly) and that was one of my first thoughts, I'll give you a 4.
CHill60 17-Mar-16 19:58pm    
I'm with Matt on this one - normally I would advise against answering old posts, but in this case you *have* offered a genuine alternative.
Well you can't return two item from method. So what you can do is, Use Dictionary.
See example below.
C#
Dictionary<string, List<string>> myLists = new Dictionary<string, List<string>>();
public void Patientlog(string Username, string Password)
{
    List1=new List<string>();
    List2=new List<string>();
     
    myLists.Add("list1", List1);
    myLists.Add("list2", List2);
 
}

// Access the list somewhere else in code
List<string> tempList = myLists["list1"];
List<string> tempList2 = myLists["list2"];

-KR
 
Share this answer
 
v4
Comments
Krunal Rohit 22-Feb-14 1:02am    
Make sure that the Dictionary should be static or having the global access.
-KR
[no name] 22-Feb-14 1:22am    
I Don't Want to access these lists to anywhere i want to simply return this lists.Because it is need to show in runtime
Krunal Rohit 22-Feb-14 9:18am    
Yeah, you can get those lists from Dictionary.
-KR
You can not return two list, but you can add both list into one and then return if it is require for you. & then you differentiate accordingly.
 
Share this answer
 
Comments
[no name] 22-Feb-14 0:57am    
I cant't add to list with another
Ashwani Gusain 22-Feb-14 3:51am    
U can make one list as global and another one can return after that u can able to access both as u wish.
Ashwani Gusain 22-Feb-14 3:57am    
Note that u cannot return more then one value at a time.
You cannot return two items using the return keyword.
The easiest thing for you to do is pass these list instances as parameters to the method.
Then assign them here and you will have them available in the calling method.

SQL
public void Patientlog(string Username, string Password, List obj1, List obj2)
{
obj1=new List();
obj2=new list();

return;

}
 
Share this answer
 
Comments
[no name] 22-Feb-14 0:16am    
then how can i get that values of Obj1 and obj2.
Abhinav S 22-Feb-14 0:19am    
Objects are passed by reference.
Try it out!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900