Novice: Returning a list

A

admin

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?
 
A

Alberto Poblacion

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, T2> which would be adequate if you use it as
Dictionary<string, int>.

For instance:

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

Jon Skeet [C# MVP]

Alberto Poblacion said:
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, T2> which would be adequate if you use it as
Dictionary<string, int>.

For instance:

private Dictionary<string, int> GetData()
{
Dictionary<string, int> results = 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>.
 
C

Cralis

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?
 
J

Jon Skeet [C# MVP]

Cralis said:
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<T> in MSDN. It's the generic version of ArrayList. As an
example, with a suitable User class, you could do:

List<User> GetUsers()
{
List<User> ret = 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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top