VB.NET ASP.NET application forgets value of variable. Is this a bug?

  • Thread starter Thread starter refer_to_website
  • Start date Start date
R

refer_to_website

How can I retain the value in memory in the ASP.NET Web application?

NOTE: This example works correctly (increments the value) when the
you create a VB.NET Windows Application, but does not work (the value
keeps getting reset to zero) when you create an ASP.NET Web
Application. Is this a bug in .NET, or am I doing something wrong?

In the ASP.NET version, I tried replacing "Dim objPerson As New
PersonDetails" with
A. "Public objPerson as New PersonDetails" (got same results)
B. "Friend objPerson as New PersonDetails" (got same results)
C. "Static objPerson As New PersonDetails" (got syntax error)


HOW TO RECREATE THE ISSUE:
Create a new VB.NET Windows application.

PROJECT | Add Class
Name = PersonDetails.vb


Public Class PersonDetails
Private m_iNumber As Integer

Public Sub AddOne()
M_iNumber = m_iNumber + 1
End Sub

Public Property CurrentValue() As Integer
Get
CurrentValue = m_iNumber
End Get
Set(ByVal Value As Integer)
m_iNumber = Value
End Set
End Property



Add this to the code-behind of Form1.vb, before any Subs or functions:
Dim objPerson As New PersonDetails()


Add a button to the form. The code behind =
objPerson.AddOne
Response.Write (objPerson.Value) 'use this for VB.NET Asp.NET
application
MsgBox (objPerson.Value) 'use this for VB.NET windows application


Each time you click on the button, it should increment the value, and
it does for the VB.NET Windows application, BUT IT DOES NOT with the
VB.NET ASP.NET application (it stays at 1). How do I get around this?
 
It's not a bug. You may want to make the property shared for instance. I
think the object is getting reset on the post back. If the property is
shared, you won't have this happen. You can also use a Session variable and
increment it each time...

HTH,

Bill
 
Back
Top