473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with ArrayList

I have an arraylist and i define it as public variable

Public StockArray As New ArrayList

inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class

StockArray.Add(stockobj)

when i print any element of Stockarray within button1 body it works fine

but when i call the stockArray in a another button body the StockArray is empty !


any ideas will be Extremly helpful
Apr 10 '07 #1
9 1659
SammyB
807 Expert 512MB
I have an arraylist and i define it as public variable

Public StockArray As New ArrayList

inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class

StockArray.Add(stockobj)

when i print any element of Stockarray within button1 body it works fine

but when i call the stockArray in a another button body the StockArray is empty !


any ideas will be Extremly helpful
Are you sure you placed StockArray in the correct place? Works for me:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Public StockArray As New ArrayList
  3.     Public iCounter As Integer = 0
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         iCounter = iCounter + 1
  6.         Dim s As New Stock
  7.         s.ID = "A-" & iCounter
  8.         StockArray.Add(s)
  9.         ListStock()
  10.     End Sub
  11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  12.         iCounter = iCounter + 1
  13.         Dim s As New Stock
  14.         s.ID = "B-" & iCounter
  15.         StockArray.Add(s)
  16.         ListStock()
  17.     End Sub
  18.     Private Sub ListStock()
  19.         Dim s As Stock, msg As String = ""
  20.         For Each s In StockArray
  21.             msg = msg & s.ID & ", "
  22.         Next
  23.         msg = Strings.Left(msg, Len(msg) - 2)
  24.         MsgBox("ArrayList contains " & msg)
  25.     End Sub
  26. End Class
  27. Public Class Stock
  28.     Public ID As String
  29. End Class
Apr 10 '07 #2
channaJ
67
I have an arraylist and i define it as public variable

Public StockArray As New ArrayList

inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class

StockArray.Add(stockobj)

when i print any element of Stockarray within button1 body it works fine

but when i call the stockArray in a another button body the StockArray is empty !


any ideas will be Extremly helpful
Hi,

Is it a web application you are working on? If it is so, I think the problem is caused by the postback. When you do a postback from the first button click,
Expand|Select|Wrap|Line Numbers
  1. Public StockArray As New ArrayList
will be called again and it will create a new empty StockArray object. You can use the following code to instanciate the StockArray only in the initial page load.
Expand|Select|Wrap|Line Numbers
  1. if(!IsPostback)
  2. Public StockArray As New ArrayList
Hope this will help.
Apr 11 '07 #3
thanks alot SammyB but this is exactly my problem the buttons ommit each other changes


and YES channaJ i'm using a web application i'm interested in your sollution

but shall I declare StockArray only once in the page_load ? because when I did the buttons could not see StockArray declaration!

and it force me to replace
Expand|Select|Wrap|Line Numbers
  1.  
  2. If (Not IsPostBack) Then
  3. Public StockArray as new ArrayList 
  4. End IF
  5.  
with this
Expand|Select|Wrap|Line Numbers
  1.  
  2. If (Not IsPostBack) Then
  3. Dim StockArray as new ArrayList 
  4. End IF
  5.  
and when I declare StockArray inside the body of Page_Load and inside the body of page Class the Array became empty :)

I'm So sorry but I'm new to asp.net with VB
Apr 11 '07 #4
SammyB
807 Expert 512MB
> I'm So sorry but I'm new to asp.net with VB
You are better than I, I've never created a web form, so Chan will have to help you there.

But, notice where I defined StockArray. It is inside the Form class, but outside of all of the Sub's. Thus, there is just one of them and all of the Sub's can access it. You should have a similar structure in your code.
Apr 11 '07 #5
Hi,

Is it a web application you are working on? If it is so, I think the problem is caused by the postback. When you do a postback from the first button click,
Expand|Select|Wrap|Line Numbers
  1. Public StockArray As New ArrayList
will be called again and it will create a new empty StockArray object. You can use the following code to instanciate the StockArray only in the initial page load.
Expand|Select|Wrap|Line Numbers
  1. if(!IsPostback)
  2. Public StockArray As New ArrayList
Hope this will help.
***********************
Hello channaJ,

I did exactly what you siad but declaring StockArray as an ArrayList inside the page_load makes StockArray undeclared to other subs inside the page calss

as you siad when not post back the array contents will be empty ,i checked that by printing the array count (StockArray.Count) and the result is zero :(

what should i do i need the array to save pervious valuse of stocks and use them when Undoing last changes.

thanks in advance
Apr 14 '07 #6
Hello every body

can any one help me with this issue i waste lots of time on it but still could not find the right sullotion any ideas :) !

regards
Apr 17 '07 #7
Frinavale
9,735 Expert Mod 8TB
I have an arraylist and i define it as public variable

Public StockArray As New ArrayList

inside button1_click body
i append new elements in that Stockarray
by
dim stockobj as new stock // stock is a class

StockArray.Add(stockobj)

when i print any element of Stockarray within button1 body it works fine

but when i call the stockArray in a another button body the StockArray is empty !


any ideas will be Extremly helpful
Hi there!

When creating a web application you have to remember that every time the user clicks a button (or causes a postback event) its as if they are coming to the website for the first time.

In your case, you're creating the stockArray ArrayList, but you are only filling it within the method that handles your button click event. Once that information is sent back to the user, all the objects are destroyed.

There is never a persistent connection to the user and the application.

This means that when the user does something else that causes a postback a new stockArray (ArrayList) is created.

In order to use information collected from a previous postback to the website you need to store your arrayList in a persistent data store....like Session.

(please note that my code is in VB.NET and is just a guideline...it probably wont work)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private stockArray As ArrayList
  3.  
  4. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
  5.  
  6.     'Since this is executed first every time we fill our stockArray 
  7.     'variable with what ever is in the Session from previous visits
  8.     'this way we can use it in the subs that handle the events
  9.  
  10.     stockArray = new ArrayList
  11.  
  12.     Try
  13.         stockArray=CType(Session("arrayListInStore"),ArrayList)
  14.  
  15.         If stockArray Is Nothing
  16.              'This is the case when the user is first visiting the website or the
  17.              'Session has expired and there is no arrayList stored in session
  18.  
  19.             stockArray = new ArrayList
  20.  
  21.              'here fill your stock array with default values if you'd like
  22.         End If
  23.      Catch ex As Exception
  24.  
  25.      End Try
  26.  
  27. End Sub
  28.  
  29. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  30.     'Here you can fill the stockArray Array list but make sure that you
  31.     'Store this in session so that you can use this data later
  32.  
  33.      'I always double check to make sure the object has been intialized
  34.       If stockArray Is Nothing Then
  35.         stockArray = new ArrayList
  36.      End IF
  37.  
  38.      stockArray.Add(....)
  39.     Session("arrayListInStore")=stockArray
  40.  
  41. End Sub
  42.  
  43.  
  44. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  45.     'Since you've already filled the stockArray variable in the page_load method
  46.     'with the information gathered during the Button1 postback (which was 
  47.     'stored in session)...
  48.     'You will be able to use the information in the stockArray variable in
  49.     'this method
  50.  
  51.      lbl_output.Text = stockArray.Length.ToString()
  52.  
  53. End Sub
  54.  

Does this make sense?

(Oh yeah, you don't need to make the variable Public unless its being used by another object. I'd recommend making it Private if its not being used by another object)

Cheers

-Frinny
Apr 17 '07 #8
pew! Finally i found it instead of declaring StockArray as public or dim declare it as shared
thanks Chan and Sammy for your time and effort :)

here is the code
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     Shared StockArray As New ArrayList
  4.  
  5.  '***********************************************************'
  6.     Public Sub Update_Group(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         'Loop through each DataGridItem, and determine which CheckBox controls
  8.         'have been selected.
  9.  
  10.  
  11.         Dim GridItem As DataGridItem
  12.         For Each GridItem In DataGrid1.Items
  13.  
  14.             Dim myCheckbox As CheckBox = CType(GridItem.Cells(0).Controls(1), CheckBox)
  15.             Dim TextBox1 As TextBox = CType(GridItem.Cells(4).Controls(1), TextBox)
  16.             Dim TextBox2 As TextBox = CType(GridItem.Cells(5).Controls(1), TextBox)
  17.  
  18.             If myCheckbox.Checked = True Then
  19.  
  20.                 Dim Stockobj As New stock
  21.                 Stockobj.FunCode = DataGrid1.DataKeys(GridItem.ItemIndex).ToString()
  22.                 Stockobj.FunPrice = Convert.ToDouble(TextBox1.Text)
  23.                 Stockobj.FunVolume = Convert.ToInt64(TextBox2.Text)
  24.                 StockArray.Add(Stockobj)
  25.                 '  Response.Write(Stockobj.FunCode)
  26.                 ' rowCount += 1
  27.                 DoUpdateStock(DataGrid1.DataKeys(GridItem.ItemIndex).ToString(), Convert.ToDouble(TextBox1.Text), Convert.ToInt64(TextBox2.Text))
  28.             End If
  29.         Next
  30.         updateDate.Text = "Last Update was on " & Now()
  31.  
  32.  
  33.         BindDataGrid()
  34.     End Sub
  35.  
  36.  
  37.  
  38.     Public Sub Undo_Update(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  39.  
  40.         Dim i As Integer = 0
  41.  
  42.         While (i < StockArray.Count - 1)
  43.  
  44.             DoUpdateStock(StockArray(i).FunCode, StockArray(i).FunPrice, StockArray(i).FunVolume)
  45.             i += 1
  46.         End While
  47.  
  48.         updateDate.Visible = False
  49.  
  50.         BindDataGrid()
  51.  
  52.     End Sub
  53.  
  54.  
Apr 17 '07 #9
Hi there!

When creating a web application you have to remember that every time the user clicks a button (or causes a postback event) its as if they are coming to the website for the first time.

In your case, you're creating the stockArray ArrayList, but you are only filling it within the method that handles your button click event. Once that information is sent back to the user, all the objects are destroyed.

There is never a persistent connection to the user and the application.

This means that when the user does something else that causes a postback a new stockArray (ArrayList) is created.

In order to use information collected from a previous postback to the website you need to store your arrayList in a persistent data store....like Session.

(please note that my code is in VB.NET and is just a guideline...it probably wont work)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private stockArray As ArrayList
  3.  
  4. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
  5.  
  6.     'Since this is executed first every time we fill our stockArray 
  7.     'variable with what ever is in the Session from previous visits
  8.     'this way we can use it in the subs that handle the events
  9.  
  10.     stockArray = new ArrayList
  11.  
  12.     Try
  13.         stockArray=CType(Session("arrayListInStore"),ArrayList)
  14.  
  15.         If stockArray Is Nothing
  16.              'This is the case when the user is first visiting the website or the
  17.              'Session has expired and there is no arrayList stored in session
  18.  
  19.             stockArray = new ArrayList
  20.  
  21.              'here fill your stock array with default values if you'd like
  22.         End If
  23.      Catch ex As Exception
  24.  
  25.      End Try
  26.  
  27. End Sub
  28.  
  29. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  30.     'Here you can fill the stockArray Array list but make sure that you
  31.     'Store this in session so that you can use this data later
  32.  
  33.      'I always double check to make sure the object has been intialized
  34.       If stockArray Is Nothing Then
  35.         stockArray = new ArrayList
  36.      End IF
  37.  
  38.      stockArray.Add(....)
  39.     Session("arrayListInStore")=stockArray
  40.  
  41. End Sub
  42.  
  43.  
  44. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  45.     'Since you've already filled the stockArray variable in the page_load method
  46.     'with the information gathered during the Button1 postback (which was 
  47.     'stored in session)...
  48.     'You will be able to use the information in the stockArray variable in
  49.     'this method
  50.  
  51.      lbl_output.Text = stockArray.Length.ToString()
  52.  
  53. End Sub
  54.  

Does this make sense?

(Oh yeah, you don't need to make the variable Public unless its being used by another object. I'd recommend making it Private if its not being used by another object)

Cheers

-Frinny
--------------------------

Hi Firnny

you know i have read lots of articles talking about session I'll try your code coze the code i have solves some problems not all of them

thanks
Apr 17 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
2
by: xmail123 | last post by:
In a web service I am using a SqlDataReader to retrieve the information to populate an ArrayList. public ArrayList ReadHistory(int ItemPK) { SDrdr = SDcmd.ExecuteReader(); ArrayList AL = new...
14
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
4
by: Stephen | last post by:
I have the following code working in order to create an array list and populate a datagrid however everytime i click my button the first item in the array and the first row in the datagrid are...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
2
by: Jarod | last post by:
Hey I change backColor of linkButton in my Page_Load: lbDoSth.BackColor = Color.Red; But I have multiView on this page. On one of the views I have detailsView. When I add a new row a set...
3
by: Arjen | last post by:
Hi, I have a public class 'settings' with a public static void 'loadsettings'. Inside 'loadsettings' I go to public static 'loadpagesettings'. Inside this function I have a problem. 1. I...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
6
by: thanigaimani83 | last post by:
Hi guys. i can explain for my doubt with example.see.my table have multiple type of employee details like as Mangemment,Programmer,Designers,Accountants,ContractEmployees.Each type of department...
2
by: Johan Öhrn | last post by:
Hi, I'm working with an asp.net 2.0 application. The application I'm working with come with it's own assemblies which calls out to code in my assembly. It may call out to my code during a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.