Some variables at the Class level not accessible in Subs

F

Fred

Following what I think are relevant portions of my code:

---------------Begin Code---------------------
Public Class LOLoadingBoard
Inherits System.Web.UI.Page
Dim MaxNum As Integer
Dim LNToEd As Integer
Dim CStatClr As Integer
Dim lBox() As Integer, lStatusColor() As Integer, lTrimWt() As Integer
Dim lPanelWt() As Integer, lPurlinWt() As Integer, lOtherWt() As Integer
Dim lWt() As Integer, lRoute() As String, lStatus() As String
Dim lTrimTime() As String, lPanelTime() As String, lPurlinTime() As
String
Dim lWalked() As String, lCompleted() As String, lBOL() As String
Dim lTrailerNum() As String, lDriver() As String, lNotes() As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
LNToEd = 3
CStatClr = 1
MaxNum = 13
Response.Write (" LNToEd = " & LNToEd & "<BR>CStatClr = " & CStatClr
& "<BR>MaxNum = " & MaxNum )
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
LNToEd = 7
CStatClr = 7
MaxNum = 7
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2Click
Response.Write (" LNToEd = " & LNToEd & "<BR>CStatClr = " & CStatClr
& "<BR>MaxNum = " & MaxNum )
End Sub
End Class
---------------End Code---------------------


You can see I have declared these vars at the Class level of this ASPX page.

Most of theses vars are available to be updated or to return their values in
ALL other subroutines under this class.

MaxNum and all of the arrayed variables are working this way.

My problem is that LNToEd and CStatClr are NOT available.

Here is an example of what I am seeing


When I run the Code it displays:

LNToEd = 3
CStatClr = 1
MaxNum = 13

If I then immediately click Button2 I get:

LNToEd = 0
CStatClr = 0
MaxNum = 13

If I then click Button1 I get:

LNToEd = 7
CStatClr = 7
MaxNum = 7

If I then click Button2 again I get:

LNToEd = 0
CStatClr = 0
MaxNum = 7


This is of coure not my entire application but this is representative of
what is happening. I don't get it. The MaxNum and array vars (once they
are Redim'ed when needed) seem to be responding the way I would expect for a
var declared with a class level scope. But just these two are giving me
troubles. I have hunted through the entire code and there are no other
places placing a 0 value in these var that I can see.

Any ideas are appreciated.

thanx
Fred
 
F

Fred

That was a Typo, didn't just copy and paste out of the app.

It should be :

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2Click
Response.Write (" LNToEd = " & LNToEd & "<BR>CStatClr = " & CStatClr
& "<BR>MaxNum = " & MaxNum )
End Sub
 
C

Cor Ligthert

Hi Fred,

I runned your code (after removing some typos more than CJ told) and it
works as it would I
expect. (I also added a dot in your last corrections)

Load gives
3
1
13
Button 1 gives
3
1
13
Button 2 gives
3
1
13
3
1
13

Strange that you get with this sample other results.
What did you expect?

Cor

....
 
F

Fred

Let's try this one and I'll include what I expected:
WebApplication
VB.Net 2003

Take Web Form and add two buttons to it.

Following is code cut and pasted :


Public Class LOLoadingBoard
Inherits System.Web.UI.Page
Dim MaxNum As Integer
Dim LNToEd As Integer
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Dim CStatClr As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MaxNum = 1
LNToEd = 2
CStatClr = 3
Response.Write("MaxNum = " & MaxNum & "<BR>")
Response.Write("LNToEd = " & LNToEd & "<BR>")
Response.Write("CStatClr = " & CStatClr & "<BR>")
Response.Write("-----------------------------------")
Response.Write(" No Buttons<BR>")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MaxNum = 4
LNToEd = 5
CStatClr = 6
Response.Write("MaxNum = " & MaxNum & "<BR>")
Response.Write("LNToEd = " & LNToEd & "<BR>")
Response.Write("CStatClr = " & CStatClr & "<BR>")
Response.Write("-----------------------------------")
Response.Write(" Button 1<BR>")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Response.Write("MaxNum = " & MaxNum & "<BR>")
Response.Write("LNToEd = " & LNToEd & "<BR>")
Response.Write("CStatClr = " & CStatClr & "<BR>")
Response.Write("-----------------------------------")
Response.Write(" Button 2<BR>")
End Sub
End Class

What I expected was:

Form Load gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons

Button 1 Press gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons
MaxNum = 4
LNToEd = 5
CStatClr = 6
----------------------------------- Button 1

Button 2 Press gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons
MaxNum = 4
LNToEd = 5
CStatClr = 6
----------------------------------- Button 2


What I got was:

Form Load gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons

Button 1 Press gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons
MaxNum = 4
LNToEd = 5
CStatClr = 6
----------------------------------- Button 1

Button 2 Press gives:
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- No Buttons
MaxNum = 1
LNToEd = 2
CStatClr = 3
----------------------------------- Button 2


If I am expecting something that shouldn't be...let me know. Then let me
know how I can modify the values of these variables. I would expect that
vars declared at the class level would be able to be modified by member
subroutines. Their values are accessible so why not modifying the values?

Fred
 
C

Cor Ligthert

Hi Fred,
If I am expecting something that shouldn't be...let me know. Then let me
know how I can modify the values of these variables. I would expect that
vars declared at the class level would be able to be modified by member
subroutines. Their values are accessible so why not modifying the values?
Yes you are probably expecting something wrong.

Form load will performed
the showing of 123
After button 1 is done
the form load showing 123
The reseting of the variables
the showing of 456
After button 2 is done
the form load is showing 123
the button 2 is showing 123 (because you do not reset anything)

A form load is always processed.
To prevent that it loads again is the the statement by the way
If not is postback then .........

Remember that you have to instance every time again, you can hold a variable
in a viewstate or in a session. (As well in a shared class and the cache
however those belongs not to the page or the session but to the application,
I would not use those 2 until you have not much expierience with the
behaviour).

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top