Return multiple values from a method

Hi,

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?
jonnytabpniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

indianguru2Commented:
Have you heard of something called ByRef

http://msdn2.microsoft.com/en-us/library/c84t73c2(VS.71).aspx
0
Gautham JanardhanCommented:
or return object []

public object[] Function()

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

Thanks
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

PockyMasterCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
}
0
gbzhhuCommented:
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).  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.