himesh
himesh

Reputation: 77

how to store multiple items in a list in c#

I am getting a list of users details and storing some of details into another list and returning that list, but the problem is at the end it is returning correct number of rows but each row has same values that is the last row's values

GetUserList usr = new GetUserList();
List<GetUserList> UsersList = new List<GetUserList>();
List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);

try
{
    if (lstusers.Count > 0)
    {
        usr.Message = "Success";

        for (int i = 0; i < lstusers.Count; i++)
        {
            usr.UserID = Convert.ToInt32(lstusers[i].userID);
            usr.UserFirstName = Convert.ToString(lstusers[i].forename);
            UsersList.Add(usr);

            //return usr;
        }
    }
    else
    {
        usr.Message = "Failed";
    }

    return UsersList;
}
catch (Exception e)
{
    throw e;
}

Upvotes: 0

Views: 1484

Answers (5)

Andrey Korneyev
Andrey Korneyev

Reputation: 26856

Just move declaration/instantiation of usr into the loop:

for (int i = 0; i < lstusers.Count; i++)
{
    GetUserList usr = new GetUserList();
    usr.UserID = Convert.ToInt32(lstusers[i].userID);
    usr.UserFirstName = Convert.ToString(lstusers[i].forename);
    UsersList.Add(usr);

    //return usr;
}

otherwise you're adding to the list reference to the same instance of GetUserList all the time and overwriting its values - so you end up with each row has same values that is the last row's values .

Upvotes: 5

sm.abdullah
sm.abdullah

Reputation: 1802

because you are updating same user every time in loop iteration and adding again and again into the list. you should create a new user every time in loop iteration

GetUserList usr ;
List<GetUserList> UsersList = new List<GetUserList>();
List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);



try
{
    if (lstusers.Count > 0)
    {


        for (int i = 0; i < lstusers.Count; i++)
        {
            //create a new instance of user here
            usr = new GetUserList();
            usr.Message = "Success";
            usr.UserID = Convert.ToInt32(lstusers[i].userID);
            usr.UserFirstName = Convert.ToString(lstusers[i].forename);
            UsersList.Add(usr);


            //return usr;
        }
    }
    else
    {
        usr.Message = "Failed";
    }


    return UsersList;
}

catch (Exception e)
{
    throw e;
}

Upvotes: 2

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Use new User object every time

GetUserList usr =null;
    List<GetUserList> UsersList = new List<GetUserList>();
    List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);



    try
    {
        if (lstusers.Count > 0)
        {
           // ;

            for (int i = 0; i < lstusers.Count; i++)
            {
                usr= new GetUserList();
                usr.Message = "Success"  
                usr.UserID = Convert.ToInt32(lstusers[i].userID);
                usr.UserFirstName = Convert.ToString(lstusers[i].forename);
                UsersList.Add(usr);


                //return usr;
            }
        }
        else
        {
            usr.Message = "Failed";
        }


        return UsersList;
    }

    catch (Exception e)
    {
        throw e;
    }

Upvotes: 2

Radin Gospodinov
Radin Gospodinov

Reputation: 2323

You are adding the same instance of GetUserList into the list. In the loop you update the fields usr.UserID usr.UserFirstName for only one instance of GetUserList class. So the effect is (lets say if lstusers.Count = 10), that you add 10 time this object to the list. Move GetUserList usr = new GetUserList(); inside the loop and usr.Message = as well.

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You have to instantiate object inside for loop, currently same object is stored in list multiple time, change your code like:

for (int i = 0; i < lstusers.Count; i++)
{
   GetUserList usr = new GetUserList(); // not this line
   usr.Message = "Success";
   usr.UserID = Convert.ToInt32(lstusers[i].userID);
   usr.UserFirstName = Convert.ToString(lstusers[i].forename);
   UsersList.Add(usr);


}

Currently you are modifying the same object every time, so the result would be the last updated content in all list items.

Upvotes: 3

Related Questions