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!

Calling a sub and incrementing a Integer within the sub 2

Status
Not open for further replies.

Govnor

Technical User
Sep 3, 2002
55
GB
Hi all,

i have a small problem,

I want to call a sub routine, and within that sub i have a Integer that I would like to increment every time I use the sub.

'variables have been declared outside the sub

Dim Arr(200) As Integer
Dim count As New Integer


Private Sub updatestatus(ByVal wipid As Integer)
Arr(count) = wipid
count += 1
Lbl_Count.Text = count
End Sub


what i am trying to do is make a array that gets populated every time the sub is called
 
Why not use an ArrayList? That way you just append a value to the end, and there's no need to keep track of the count.
Code:
Imports System.Collections

Dim m_WiPidArrayList As ArrayList

Private Sub updatestatus(ByVal wipid As Integer)
   m_WiPidArrayList.Add(wipid)
    Lbl_Count.Text = m_WiPidArrayList.Count
End Sub
The "m_" prefix indicates a module-level variable -- one that has been declared at the class level, and not inside any particular method.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Cheers, I will give that a try. But sounds like the right solution to me. ;-)

THE GOV' NOR
 
Hi,

Another quick question on the same topic.

I like the idea of ArrayList

But how do I sort through the ArrayList to perform a task on each item once I have populated it the ArrayList?

I know you can use the following code with an Array

For i As Integer = 0 To m_WiPidArrayList.Length - 1

‘Do some code here

Next i


THE GOV' NOR
 
If you need to do something to every item in an ArrayList, you can use the foreach statement. It acts like a for..next, but it explicitly functions on every item in a collection (or anything else that implements IList).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I get this error, a little help!!!!

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 111:
Line 112: Private Sub updatestatus(ByVal wipid As Integer)
Line 113: WiPidArrayList.Add(wipid)
Line 114: Lbl_Count.Text = WiPidArrayList.Count
Line 115: Lbl_Wipidinfo.Text = String.Concat(Lbl_Wipidinfo.Text, Convert.ToString(wipid), ", ")


Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
AIS.AIS.Admin.Admin.updatestatus(Int32 wipid) in C:\Inetpub\ AIS.AIS.Admin.Admin.Datagrid_wipInformation_UpdateCommand(Object source, DataGridCommandEventArgs e) in C:\Inetpub\ System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +109
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +507
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +106
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +26
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +115
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277




THE GOV' NOR
 
In debug mode I can see wipid = 49

THE GOV' NOR
 
Did you declare a module level ArrayList as Chip said?
Dim WiPidArrayList as ArrayList
Marty
 
Yes, I sure did...


Dim WiPidArrayList As ArrayList
Dim SortArrayList As New ArrayList

Private Sub updatestatus(ByVal wipid As Integer)
WiPidArrayList.Add(wipid)
Lbl_Count.Text = WiPidArrayList.Count
Lbl_Wipidinfo.Text = String.Concat(Lbl_Wipidinfo.Text, Convert.ToString(wipid), ", ")
End Sub



'once I have populated the ArrayList I want to sort through it

For Each SortArrayList In WiPidArrayList

' some code here to view and edit value of SortArrayList

Next



'hope this makes sense

THE GOV' NOR
 
fixed by calling

Dim WiPidArrayList As ArrayList

should be

Dim WiPidArrayList As New ArrayList

but still cannot

Lbl_Count.Text = WiPidArrayList.Count

as the count always equals 0, don't know why

any ideas anyone

THE GOV' NOR
 
It may be a silly question, but are you sure you're adding things to the arraylist during the execution of that page load? If you call your update method and then it waits across a postback, when the page is rendered again (I'm assuming this is ASP.NET because it's in this forum) the arraylist you declared will be instantiated again as a new one - values don't persist across postbacks unless you store them in the viewstate or session.
 
Hi,

well i click a update button on a datagrid which in turn calls this updatestatus function.

So you maybe right that the arraylist I declared will be instantiated again as a new one.

how do i solve this ???

If I store them in the viewstate or session, how should i implement this ???

Cheers :)



THE GOV' NOR
 
if you want to store it in the session, you could instantiate an arraylist in session_start of Global.asax with:
Session.add("myarraylist", new arraylist)

then when you want to retrieve it to get something from it or add something to it in your page you could use:

dim al as arraylist = directcast(session("myarraylist"), arraylist)

then use al as your arraylist, and because it will be a reference to the one stored in your sessionstate it will persist across postbacks and not be recreated every time.

I don't know if this is where your exact problem lies, but I hope this helps :)


 
Hi all,

what I decide to do with dace advice is use sessions.


So on the page load I wrote


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("WipListArr") Is Nothing Then
InitializeArray()
End If

End Sub

this calls the InitializeArray() function if the session is empty

Public Sub InitializeArray()
Dim WiPidArrayList As New ArrayList
Session("WipListArr") = WiPidArrayList
End Sub

hence I make a new instance

then when I click the updatestatus I update my array


Private Sub updatestatus(ByVal wipid As Integer)
Dim WiPidArrayList As ArrayList = Session("WipListArr")
WiPidArrayList.Add(wipid)
Lbl_Count.Text = WiPidArrayList.Count

End Sub


then when I confirm, i can use the populated array to do some functions, i.e. enter info into the database

Private Sub confirm()
Dim WiPidArrayList As ArrayList = Session("WipListArr")

Dim en As IEnumerator = WiPidArrayList.GetEnumerator

While en.MoveNext

'do some functions here using the en.Current value

End While

Response.Redirect("Confirm.aspx")
End Sub


Thanks to chiph, dance and everyone else for your help...


THE GOV' NOR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top