Return multiple values from a method


I am creating a small application with a database class to handle all db requests in c#. The part I am stuck on is querying the database.

I am using the MySQL .net connector and can so far query the database but cannot find a way to return the rows through the class? I was thinking of returning it just through the method but I've realised that it can only return one function.

Anyone have any ideas on what I could do?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

PockyMasterConnect With a Mentor Commented:
I usually have my datalayer translate a set of rows into a collection of DataEntities.
The database call would then e.g. return a List<Person>

A Person class would have properties like Name, Age and such and would know how to transform itself from a datarow into itself (by implementing a static method on the person class)

The DAL method would get a dataset out of the database, and loop through the rows and call the transformation method for each row and would add a new person to the result list.
When done, return the list.
Have you heard of something called ByRef
Gautham JanardhanCommented:
or return object []

public object[] Function()

Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

jonnytabpniAuthor Commented:
Could you elaborate on your solutions please? I'm fairly new to C# and a little more help would be appreciated.

Gururaj BadamCommented:
There are two ways you can return multiple values from a method.

1. ByRef
2. Out

ByRef is basically when you populate with initial values into IN parameters whereas out is when you just expect the results but don't have any initial data to popoulate the parameters. ject references.

public void Add( int x, int y, out int sum)
  sum = x + y;

public void CopyTo( MyClass x, ByRef MyClass target)
  target = new MyClass( x ); // assuming that MyClass supports creating new instances based on object instance
gbzhhuConnect With a Mentor Commented:
I support PockyMaster's idea.  I usually have a Busines Layer.  The DAL returns dataset/datareader (it doesn't do transformation)  The Business Layer contains classes that represent your database objects. i.e employee, order, product as well general classes like say UserSession which will include your collections List<Employee> List<Order> etc.

Now your GUI will create a new UserSession then call something like GetOrders of UserSession.  The GetOrders method will fire the DAL, get a dataset/datareader back.  Iterate through that and fill it's List<Order> member. The GetOrders method returns true/false for success/failure.  On returning true you can access the collection.  Now at any time later on other forms I can still access UserSession's collections (this is useful for info that doesn't change very often).  
All Courses

From novice to tech pro — start learning today.