Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList keeps getting re-initialized!!!

Status
Not open for further replies.

FederalProgrammer

Programmer
Jul 2, 2003
318
CA
I am trying to add a bunch of objects to my Listbox using an arraylist using the follwoing 2 funx:

Code:
Private Sub Page_Load(ByVal s As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'If (Not Me.IsPostBack) Then
        Me._list = New ArrayList
        Me.ListBox1.DataSource = Me._list
        Me.ListBox1.DataBind()
        'End If

    End Sub

Code:
Private Sub btnAddBandMember_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBandMember.Click
        Dim newObject As New SomeObject()
        Me._list.Add(newObject)
        Me.ListBox1.DataBind()

    End Sub

My problem is that me._list always contains a single object!!! what's going on with this thing?



---------------
 
The ArrayList doesn't maintain its state over PostBack. My VB.NET syntax may be off, but you want something like:

Code:
Private Sub Page_Load(ByVal s As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'always initialize the ArrayList
        Me._list = New ArrayList

        If (Not Me.IsPostBack) Then
          
          Me.ListBox1.DataSource = Me._list
          Me.ListBox1.DataBind()
        Else
          For Each item In Me._list.Items
	    Me._list.Add( item )
          Next
        End If

    End Sub

Code:
   Private Sub btnAddBandMember_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddBandMember.Click
        Dim newObject As New ListItem()
        Me._list.Add(newObject)
        Me.ListBox1.DataBind()
    End Sub
 
hay, thanx for the reply,

Code:
 If (Not Me.IsPostBack) Then
         
     Me.ListBox1.DataSource = Me._list
     Me.ListBox1.DataBind()
 Else
     For Each item In Me._list.Items
        Me._list.Add( item )
     Next
 End If

When the "Else" statement is being executed, me._list is empty and therefore the loop doesn't get executed...

any ideas?

ps, you can answer me in C# too (if it's easier for you)



---------------
 
well, in case any one's interested, I used the "Cache" to store my arraylist during postbacks. I hope this is not bad programming.

Web programming is a new concept for me, even though I've been writing windows apps for the past 10 years... so i really want to follow good programming prgactices...
Please let me know if there's a better way to do this!!

cheers!



---------------
 
Oops.

Me._list.Items

should have been:

Me.ListBox1.Items

in the last snippet.

The ListBox items should already be in ViewState so you can probably save a little memory and forego the Cache (plus you wouldn't have to worry about expiration and stuff like you would with the Cache).
 
thanx again for replying... what you say makes sence!! however, what is being added to the listbox is an object. I cannot retrieve this object from the listbox (since it seems like each item added to a listbox MUST be a string). So retriving what is in the listbox will give me a mere string (rather than the actual object (with its properties and attributes) that was added to the listbox):

Code:
dim Obj as someObject
MyArrayList.Add(Obj)
me.listBox1.DataSource = me.MyArrayList

so as you can see, if I go through the items in listBox1, I CANNOT do the following:

Code:
dim item as someObject
for each item in me.ListBox1.items
'some code
next item

I get an invalidCast Exception since type of item is someobject and items in me.ListBox1 have a string type! I hope I make sence...



---------------
 
Do you necessarily need to persist the values in object format? If you only need to track the ListBox values, you can just translate the initial values into ListItem form, then keep track of everything that way.

e.g.

Code:
private ArrayList al;

private void Page_Load(object sender, System.EventArgs e)
{
	al = new ArrayList();
	ListBox1.DataTextField = "Text";
	ListBox1.DataValueField = "Value";

	if( !Page.IsPostBack )
	{
                //presumes an object that has a string and int property
                MyObject mo = new MyObject( "string property", 1 );
		al.Add( new ListItem(
                           mo.StringProperty, 
                           mo.IntProperty.ToString() ) );
		al.Add( new ListItem( "text...", "2" ) );
		al.Add( new ListItem( "more stuff", "3" ) );

		ListBox1.DataSource = al;
		ListBox1.DataBind();
	}
	else
	{
		foreach( ListItem li in ListBox1.Items )
			al.Add( li );
	}
}

private void Button1_Click(object sender, System.EventArgs e)
{
	al.Add( new ListItem( "some text", "some value" ) );
	ListBox1.DataSource = al;
	ListBox1.DataBind();
}

You may also have the option of translating the .Items to your object type then binding if you'd rather (assuming you don't need to track other associated info).
 
I really have to keep my objects. You see these objects have many data members and properties each of which is made up of other objects (a true object oriented code).




---------------
 
I see. You're right then, your options are really just caching and Application variables (or Session if it's user-specific, of course). As I'm sure you know, the only thing you have to be careful of is that the cached objects don't expire before you do what you want to them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top