Return several objects from a function

I have this function (below) that fetches posts from a database. At the moment I put each resulting post in a row that's part of a string. The string is returned by the function.

What I would like to do is to have a class that has the fields:
- ID (integer)
- Fnamn (string)
- Enamn (string)
- Fdatum (date)
and put each post from the DB in one such object. Then I want to put all these objects either in one object that holds all objects (which I believe is the better solution) OR in an array of objects. Then I want the function to return this object of objects (or array of objects).

I have tried a lot of different (obviously incorrect) ways of doing this and have not succseeded, thus the returning of a string.

I have a module named module1 where I put definitions that are needed in different parts of the app, I've tried to put a class definition in it. (code below).

Re: Return several objects from a function

Posted 26 October 2008 - 09:26 AM

Isn't that what datasets are used for? A dataset has a datatable.. a datatable has data columns.. your columns are named for the return values you are getting from the database.. every record's in row format.

Side note, do not do SELECT * from the database.. it's a bad habit to get into, and will one day bite you in the ass (when you return more columns than you are taking care of or less than you were initially).

Re: Return several objects from a function

Posted 26 October 2008 - 12:38 PM

dbasnett - thanks!

modi123 - I'll try to keep the "select *"-stuff in mind, thanks. About using datasets or business objects there are different views. What I'm presenting here is a simplification of my real problems. A link if you like: http://www.kellerman...essobjects.aspx

Re: Return several objects from a function

Posted 26 October 2008 - 01:41 PM

I think you are on the right track with your person class, the part you are missing is you want to store each instance of person in a COLLECTION. So as you loop through reading your results from the database you create a new instance of person class set its values then add that class to a collection of person.

There are many benifits to using classes in this fashion, as you can easily add new methods and properties to the class that can be used by your application. As an example add a property FullName that concats the first and last name.

I tried my best to modify your load routine to return a collection instead of your string.

Re: Return several objects from a function

Posted 26 October 2008 - 04:18 PM

In my example above I returned it as a generic collection List(of personClass), look at the end of the function to see. This is the easiest way to make a collection, if you wanted to add your own methods properties to the collection then you could inherit the generic collection instead.

Public Class Persons
Inherits List(Of personClass)
Public Sub MyOwnMethod()
End Sub
End Class