|
-
Jun 2nd, 2011, 01:16 PM
#1
Thread Starter
Fanatic Member
Question on Lists
I asked a question the other day on this subject, but I think I made it too complicated.
In my DataObjects class I have a class called User. Which, to keep things simple, let's say looks like this.
Code:
public class User
{
private int _UserID;
public int UserID
{
get { return _UserID; }
set { _UserID = value; }
}
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
}
In my DataAccess class I want to return a list of users, so I might have this:
Code:
public List<DataObjects.User> getUsers()
{
List<DataObjects.Event> myUsersList = new List<DataObjects.Event>();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = myConnection.CreateCommand();
SqlDataReader dr;
myCommand.CommandText = "getUsers";
myCommand.CommandTimeout = 120;
myCommand.Connection = myConnection;
myCommand.CommandType = CommandType.StoredProcedure;
myConnection.Open();
dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
while (dr.Read())
{
DataObjects.User myUser = new DataObjects.User();
myUser.UserID = (dr["UserID"] == null) ? 0 : Convert.ToInt32(dr["UserID"].ToString());
myUser.UserName = (dr["UserName"] == null) ? "" : dr["UserName"].ToString();
myUsersList.Add(myUser);
}
}
return myUsersList;
}
And, on any .aspx page I can get my list of users by writing this:
Code:
DataAccess da = new DataAccess();
List<DataObjects.User> myUsersList = new List<DataObjects.User>();
myUsersList = da.getUsers();
gvUsers.DataSource = myUsersList;
gvUsers.DataBind();
Which all works fine.
So, my question is ... what does the code snippet mean in the context of the Data Objects class? I have a User Object and I can create a list of UserObjects easily enough (as above) ... so what does the code below mean or represent?
Code:
public class UsersList : List<User>
{
}
I assumed it was effective a sort of shortcut so that, for example, in the DataAccess class instead of creating the List like this ..
Code:
List<DataObjects.Event> myUsersList = new List<DataObjects.Event>();
... I thought you might be able to create it (something) like this:
Code:
UsersList myUsersList - new List<UsersList>;
... but all I get are syntax errors. Have I missed something fundamental here?
Thanks for any help.
-
Jun 2nd, 2011, 01:31 PM
#2
Re: Question on Lists
Code:
public class UsersList : List<User>
{
}
This means you are creating a new class that Inherits from List<User> so instead of instantiating a new List<UsersList> I think you should instantiate a new UsersList and have a constructor in this new class.
Code:
UsersList myUsersList = new UsersList();
Also, you need an equal sign '=' not a minus sign '-' and the parenthesis at the end But I'm pretty sure this was not the source of your error anyway.
 Originally Posted by Webskater
Code:
UsersList myUsersList - new List<UsersList>;
.
Last edited by stlaural; Jun 2nd, 2011 at 01:37 PM.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Jun 2nd, 2011, 02:57 PM
#3
Thread Starter
Fanatic Member
Re: Question on Lists
 Originally Posted by stlaural
Code:
public class UsersList : List<User>
{
}
This means you are creating a new class that Inherits from List<User> so instead of instantiating a new List<UsersList> I think you should instantiate a new UsersList and have a constructor in this new class.
When you say 'have a constructor in this new class' ... like this?
Code:
public class UsersList : List<User>
{
UsersList myUsersList = new UsersList();
}
I tried doing that and then put this on a .aspx page ...
Code:
UsersList myUsersList = new List<UsersList>();
but when I try to run it I get this ...
Cannot implicitly convert type 'System.Collections.Generic.List<DataObjects.UsersList>' to 'DataObjects.UsersList'
Thanks for your help. (The minus sign etc. were typos - I typed the code up changing it to be about 'Users' just to make it more generic.)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|