Class Person
..class properties
..class methods
End Class
Dim Person As New Person
Person.GetPersonInformation(Session("PersonId"))
Problem: Person Object will persist upon posting back, even after
creating a new Person
Question: I thought state is not held between postbacks
> Problem: Person Object will persist upon posting back, even after creating a new Person
Have you tried calling Person.Dispose() when you're done with it ?
Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
<njk...@yahoo.com> wrote in message news:1154023912.4...@m79g2000cwm.googlegroups.com...
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"njk...@yahoo.com" wrote:
> I have a dynamically loaded control that queries a database and fills
> the results into a new object.
>
> Class Person
> ...class properties
> ...class methods
Juan, This is my own simple object I instantiate, therefore I do not
have a dispose method???
Peter, I'm not saving the object to Session at anytime. Here are some
steps:
1. ASPXPage.LoadControl("PersonControl.ascx")
2. Call Sub BuildPerson - Create Person, Retrieve Age
3. Post Back and step through
a. Create Person --Age persists
Class Person
Property Age
Public Function GetPersonInformation()
...get age from database
... set property Age
End Function
End Class
Sub BuildPerson
Dim Person As New Person
Person.GetPersonInformation()
dim age=Person.Age
End Sub
You are retrieving a value from Session, according to what you wrote :
Dim Person As New Person
Person.GetPersonInformation(Session("PersonId"))
So, you must have *previously* saved Session("PersonId"), right ?
re:
> Juan, This is my own simple object I instantiate,
> therefore I do not have a dispose method?
If your object implements IDisposable ( and quite a few created objects do ),
unless you dispose of the object, you'll have to wait until GC ( garbage collection )
gets rid of it for you...unless you dispose of it with objectname.dispose().
You might need to *implement* .dispose() for your object.
See :
http://zones.advisor.com/doc/16573
...in particular, and :
http://www.informit.com/articles/article.asp?p=25751&seqNum=3&rl=1
http://www.informit.com/articles/article.asp?p=101592&seqNum=4&rl=1
http://www.velocityreviews.com/forums/t78250-re-dispose-pattern-in-aspnet-pages.html
...as background info.
Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
--
-Demetri
re: Session... I'm saving just an Id in session not the Person object
re: Dispose... I do close the SQLconnection immediately in my Person
object. ( its my Person "object that lives between postbacks" per your
last article
--http://www.velocityreviews.com/forums/t78250-re-dispose-pattern-in-aspnet-pages.html
)
Demetri has it nearly correct when he says "it appears you're building
the
Person class on each Page_Load event execution of the
PersonControl.ascx
page. That would explain why it appears to be persisting accross
postbacks."
Its not actually in the Page_Load event, but I am building the Person
Class each time I load my control
(ASPXPage.LoadControl("PersonControl.ascx")
Question: Per your last article and Demetri's comment ("...across
postbacks"). Would it be reliable to use that data that persists across
postbacks, thereby eliminating the need
to create Person object and hit the database connection again. Because
I actually have another Sub that uses Person data upon postback or is
there a way to not persist between postbacks. Thanks
Sub AnotherSub {
Dim Person As New Person
if Person.Age = Nothing then Person.GetPersonInformation()
else
dim age=Person.Age
}
Class Person
Implements IDisposable
Property Age as integer
Sub GetPersonData()
SQLCn.Open
Me.Age = DS.Item("Age")
SQLCn.Close
End Sub
Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
Demetri seemed to hit the nail on the head when he wrote,
"If I understand what you wrote correctly, then it appears you're
building the Person class in each Page_Load event execution of the
PersonControl.ascx page. That would explain why it appears to be
persisting across postbacks."
Question 1: Where is it persisting the Person object?
Question 2: How long does it persist?
Question 3: Would it be reliable to use that data?
Question 4: If not reliable, how do I destroy the object?
"State refers to the persistence of an object (class instance or
primitive) in memory Whenever an object is instantiated (created), it
must reside in memory somewhere. The lifetime of an object is limited
to the lifetime of the container in which it resides. A method is
instantiated, just like a variable, whenever you call it. And just like
a variable, when it returns, it is available for garbage collection.
The only way to persist an instance is to put it into something else
that persists. Hence, ASP.Net, which operates using HTTP, which is
stateless, has mechanisms for maintaining state, such as Session,
Application, and ViewState."
Question 1: Where is it persisting the Person object? --on the stack
Question 2: How long does it persist? --until GC or I dispose
Question 3: Would it be reliable to use that data? --No
Question 4: If not reliable, how do I destroy the object?
As indicated from my previous posts, I did try to Implements a dispose
on the object. Does anyone see what I am doing wrong?
I mistakenly declared the variable Age as Private Shared instead of
just Private. Now the GC does the job correctly. Thanks to all that
replied.