Skip to content

Problem with ArrayList

kourosh saleh

Asked Feb 14, 2006, 2:15 AM EST4 answers
Share
LinkedInX
Hi,

I have a little problem with ArrayList that make me crazy,
I have a usual Web Application that contains two Buttons,

When i click on Button1 it would add an object to MyArrayList and Button2 Shows the Objects.
but MyArrayList.Count is Zero allways.

here is my code:
public partial class DoFeed : System.Web.UI.Page
{
     private ArrayList TheItems = new ArrayList();
    
     protected void Page_Load(object sender, EventArgs e){  }

     protected void Button1_Click(object sender, EventArgs e) 
    {             
        OneItem Item = new OneItem();
        Item.Title = TitleTXT.Text;
        Item.Link = LinkTXT.Text;

        TheItems.Add(Item);
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        int m = TheItems.Count; //it is zero allways
        for(int i=0; i<TheItems.Count i++)
         .......
    }
}

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hi,

    Do the modifications as below and it should work.

    public partial class DoFeed : System.Web.UI.Page
    {
         private ArrayList TheItems = new ArrayList();
         // Declaration should go here......
         OneItem Item;
        
         protected void Page_Load(object sender, EventArgs e){  }

         protected void Button1_Click(object sender, EventArgs e)
        {            
            //OneItem Item = new OneItem();

     // Instantiate here.... 
      Item = new OneItem();
            Item.Title = TitleTXT.Text;
            Item.Link = LinkTXT.Text;

            TheItems.Add(Item);
        }
        
        protected void Button2_Click(object sender, EventArgs e)
        {
            int m = TheItems.Count; //it is zero allways
            for(int i=0; i<TheItems.Count i++)
             .......
        }
    }

    Regards,
    Mohammed Sajid

    0
  • 0
    I think, what Mohammed Sajid said are corrected , because kourosh saleh declaration "item" varaiable in function of button1_click(), so the functionc button2_click() can't not see "item" varaiable. If you declaration "item" varaible in Class Dofee and instance in function button1_click() samed with Mohammed's demo, result will count number sub-item of  Item.

    Regards,
    ThangTruong
    0
  • 0
    Thank you,

    I have tested it but it doesn't work,
    the problem is about "state" when you make a event in a web page all controls were recreated and you must save the state, by session or ViewState,

    I have solve my problem thanks,

    I am not good to explain,
    0
  • Anwar Buchoo
    0
    It's nice that you've found a way out of it.

    But, it's not always advisable to save data into Session or ViewState object as this can slow down the loading of the web page and consume memory for a amount of time.

    Think, instead, of using a hidden field within your web form (.aspx) to save the data.


    Rgds.

    NB: Just for information, the data of variables are lost when the form is posted back.
    0