467,910 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Novice: Returning a list

I have a class that has a function that should query a database and
return a list of usernames as well as their id. What type should I use
as a return type, that can hold data such as:

user1, 1
user2, 2
user3, 3

?

Maybe an array, but I am lost in how to use one here. Is there no List
type that I can return, and then the calling procedure can do a
foreach on the list and populate a drop down list?

May 6 '07 #1
  • viewed: 1570
Share:
5 Replies
<ad***@myschoolmates.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
>I have a class that has a function that should query a database and
return a list of usernames as well as their id. What type should I use
as a return type, that can hold data such as:

user1, 1
user2, 2
user3, 3

?

Maybe an array, but I am lost in how to use one here. Is there no List
type that I can return, and then the calling procedure can do a
foreach on the list and populate a drop down list?
If you are using the Framework 2.0 (Visual Studio 2005), you can make
good use of Generics. The namespace System.Collections.Generics contains a
class named Dictionary<T1, T2which would be adequate if you use it as
Dictionary<string, int>.

For instance:

private Dictionary<string, intGetData()
{
Dictionary<string, intresults = new Dictionary<string, int>();
results.Add("user1", 1);
results.Add("user2", 2);
results.Add("user3", 3);
return results;
}

May 6 '07 #2
Thanks very much. That's exactly what I needed.

May 6 '07 #3
Alberto Poblacion <ea******************************@poblacion.org>
wrote:
Maybe an array, but I am lost in how to use one here. Is there no List
type that I can return, and then the calling procedure can do a
foreach on the list and populate a drop down list?

If you are using the Framework 2.0 (Visual Studio 2005), you can make
good use of Generics. The namespace System.Collections.Generics contains a
class named Dictionary<T1, T2which would be adequate if you use it as
Dictionary<string, int>.

For instance:

private Dictionary<string, intGetData()
{
Dictionary<string, intresults = new Dictionary<string, int>();
results.Add("user1", 1);
results.Add("user2", 2);
results.Add("user3", 3);
return results;
}
That's not a good general solution:

1) It loses the ordering of the list
2) It assumes uniqueness of the first element.

Now in fact for a username, that's probably not a problem - but a far
better general purpose solution would be to create a User type with the
name and ID, and then return a List<User>.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 6 '07 #4
Thanks John.

Could you point out a simple example if this being done? Seems like a
good idea. So you're creating a class, 'User', and a list of these
users, then returning this list? What help can I look out for with
regards this List?

May 7 '07 #5
Cralis <ad***@myschoolmates.comwrote:
Could you point out a simple example if this being done? Seems like a
good idea. So you're creating a class, 'User', and a list of these
users, then returning this list? What help can I look out for with
regards this List?
Look up List<Tin MSDN. It's the generic version of ArrayList. As an
example, with a suitable User class, you could do:

List<UserGetUsers()
{
List<Userret = new List<User>();
ret.Add (new User("Jon", 1));
ret.Add (new User("Douglas", 2));
return ret;
}

foreach (User user in GetUsers())
{
Console.WriteLine (user.Name);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by waters | last post: by
16 posts views Thread by ranger | last post: by
3 posts views Thread by Greg Smith | last post: by
5 posts views Thread by Robert Fitzpatrick | last post: by
14 posts views Thread by Fabian Steiner | last post: by
9 posts views Thread by Hank Stalica | last post: by
5 posts views Thread by =?ISO-8859-2?Q?Istv=E1n?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.