Hjgfhjghj Hjghjghj
Hjgfhjghj Hjghjghj

Reputation: 3

C# Adding item into List<> doesn't increase its size

I'm playing a little bit in ASP.NET and here is some code to display list size and some buttons to manipulate it.

I have separated class to create the list with a constructor. Here is also a method to increase its size

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class Class1
{
    public List<byte> Pole { get; set; }
    public int velikost_pole { get; set; }  //list size

    public Class1()                         //constructor, default size is 3
    {
        List<byte> pole = new List<byte>();
        for (byte a = 0; a < 3; a++)
        {
            pole.Add((byte)(a * a));
        }
        Pole = pole;
        velikost_pole = Pole.Count;
    }
    public string zvetsit_pole()            //method to increase the list size by adding item
    {
        if (Pole.Count < 6)                 //max size
        {
            Pole.Add((byte)(Pole.Count*Pole.Count+1));
            velikost_pole = Pole.Count;
            return "Pole zvetseno o 1";
        }
        else
        {
            return "Jiz nelze zvetsit";
        }
    }
}

In the main file, the object is created as global variable and there is a button to increase the list size. However, the size doesnt change and its still the same

public partial class _Default : System.Web.UI.Page
{
    Class1 objekt = new Class1();                   //create the object

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Velikost pole je: " + objekt.velikost_pole.ToString() + "<br />";
        for (int a=0;a<objekt.velikost_pole;a++)
        {
            Label1.Text += objekt.Pole[a].ToString() + "&nbsp";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)    //call method to increase the size
    {
        Label1.Text = objekt.zvetsit_pole();
    }

}

Thanks for ideas.

Upvotes: 0

Views: 266

Answers (2)

fpintos
fpintos

Reputation: 191

Each request creates a new instance of the _Default class, and correspondingly, objekt will be a new instance of Class1 as well.

This means that any change you make to objekt in one request will not be reflected in the next request.

In order for the changes to be persisted between requests you will need to store them somewhere. There are different options to do that, some involve using the user's session, others using the page's view state or maybe persisting the state to disk. Which one you choose depends on what exactly you want to accomplish.

For additional info, see pages like these:

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

Remember that ASP.NET is stateless so each request creates a completely new Page object. So your activity timeline looks like this:

  • Request page
    • Page created
    • objekt created
    • page rendered
  • Button1 clicked
    • Page created
    • objekt created (not the same object as the first reqest)
    • Label1.Text set
    • page rendered
  • Button2 clicked
    • Page created
    • objekt created (not the same object as either of the first two reqests)
    • zvetsit_pole called
    • Label1.Text set to return value
    • page rendered

So the button click events are dealing with different objects, so you don't see the results of one action in the next action.

The answer is to put objekt in some sort of persistent storage like ViewState, Cache, or Session. If you don't know which is appropriate for your needs do some research and decide for yourself.

Upvotes: 7

Related Questions