need Help on a Class livetime under ASP.NET

  • Thread starter Bruno Alexandre
  • Start date
B

Bruno Alexandre

Hi guys,

I'm using a session to save an ArrayList, so I do not read Database
everytime user reload the page or enter the site (the Data is consistent for
all entire session time when the user is on the site).

I'm using a Class called ProductBoxes and inside I have a public property
of Object type and I call from the PageLoad event

dim boxes as new ProductBoxes
boxes.ProductSession = session("something")

and it works ok, with all the subs and function of the class, so I can
populate the page with 5 boxes (images) in the way I want.

but when I raise btnLeft_OnClick HOW CAN USE A SUB UNDER my class?

Protected Sub btnMoveLeft_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
boxes.moveLeft()
End Sub

if I do:
dim boxes as new ProductBoxes
boxes.moveLeft()

I get an error when I use the session... because I'm creating a NEW instance
of the object and I need to use the old one, the old one have the session
with the data that I need :(

how can I do this? or all the Data in a Class as the life of a "refresh"
page? do I need to always save the data in a HiddenField so I can get it
again and populate the new instance??? but that's crazy :( isn't it?




--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)
 
K

Karl Seguin [MVP]

private boxes as ProductBoxes

sub page_load(...)
boxes = new ProductBoxes()
boxes.ProductSession = session("something")

end sub

Protected Sub btnMoveLeft_Click(...)
'you can use boxes here because it's already been created and the session
loaded in page_load
End Sub


Karl
 
B

Bruno Alexandre

I can't do that... it gives me an error (I thought it worked like that)

Public Class ProductBoxes
Private _session As Object
.....

Public Property prdSession() As Object
Get
Return _session
End Get
Set(ByVal value As Object)
_session = value
End Set
End Property

and the funcion starts with

Public Sub moveLeft()
Dim listImages As ArrayList = _session
Dim listCount As Integer = listImages.Count <<<-----
Dim currentMiddleImg As Integer = CInt(_middleImg.Value)


The error points to the <<<----- line, saying
System.NullReferenceException: Object reference not set to an instance of an
object.

in other words _session does no longer have the session("something") data


any ideias?



--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:%[email protected]...
 
K

Karl Seguin [MVP]

I guess I'm a little unclear..but the solution might be to do:

private _session as Object
public propert prdSession() as object
get
if _session is nothing then
_session = Session("something")
end if
return _session
end get
set
_session = value
Session("something") = value
end set
end property


and always access the value via your property, and not your field..

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/


Bruno Alexandre said:
I can't do that... it gives me an error (I thought it worked like that)

Public Class ProductBoxes
Private _session As Object
....

Public Property prdSession() As Object
Get
Return _session
End Get
Set(ByVal value As Object)
_session = value
End Set
End Property

and the funcion starts with

Public Sub moveLeft()
Dim listImages As ArrayList = _session
Dim listCount As Integer = listImages.Count <<<-----
Dim currentMiddleImg As Integer = CInt(_middleImg.Value)


The error points to the <<<----- line, saying
System.NullReferenceException: Object reference not set to an instance of
an object.

in other words _session does no longer have the session("something") data


any ideias?



--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:%[email protected]...
 
B

Bruno Alexandre

still no luck :(

but I only set boxes.prdSession one time when the page is loaded for the 1st
time...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)


If Not Page.IsPostBack Then

boxes.image01 = imgPrd01
boxes.image02 = imgPrd02
boxes.image03 = imgPrd03
boxes.image04 = imgPrd04
boxes.image05 = imgPrd05
boxes.middleImage = middleImgNumber
boxes.prdBoxID = imgPrdName
boxes.prdDesName = prdDescName
boxes.prdDesContent = prdDescContent
boxes.prdValue = prdValue
boxes.ProductType = "BOXES"
boxes.prdSession = Session(boxes.ProductType)

boxes.presentImages()

End If


End Sub


Protected Sub btnMoveRight_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
boxes.moveRight()
End Sub

Protected Sub btnMoveLeft_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
boxes.moveLeft()
End Sub


-----------------------------------------------------

When you enter the page, everything is fine... all the 5 images are
presented the way they need to be

when you invoke the btnMoveRight_Click and it enters in the Class Sub
moveRight or moveLeft it gives an error in the second line, the line that
needs the ArrayList.count and with a little bit of code I can see that
_session no longer have the ArrayList :-(

Private _session As Object

Public Property prdSession() As Object
Get
Return _session
End Get
Set(ByVal value As Object)
_session = value
End Set
End Property

Public Sub moveLeft()
Dim listImages As ArrayList = _session
Dim listCount As Integer = listImages.Count <---------- ERROR
Dim currentMiddleImg As Integer = CInt(_middleImg.Value)
...

-----------------------------------------------------

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

Line 181: '_session = getAllImagesFromArray(_type)
Line 182: Dim listImages As ArrayList = _session
Line 183: Dim listCount As Integer = listImages.Count
Line 184: Dim currentMiddleImg As Integer = CInt(_middleImg.Value)
Line 185: Dim aItens As Array

Source File: E:\_Trabalhos\_Web\101. FQ Scandinavia Demos\Intercell
(VS2005)\App_Code\ProductBoxes.vb Line: 183

-----------------------------------------------------

My question is:
Why don't the class keep the _session value like it keeps for all of the
others...?
Do I need to set all values again on each event?

Because in Window Forms, the Class keep all the values since they are set
until the user get's out the application.



Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)
 
K

Karl Seguin [MVP]

in a webform, each request is like the user opening and closing an
application - they are all served by separate threads and are stateless
(except for things like cookies and sessions). You need to move
boxes.prdSession = Session(boxes.ProductType) outside of the IF block. This
line of code needs to be executed on Postback as well - not just during the
initial hit.

Karl
 
B

Bruno Alexandre

so why using Class's in ASP.NET?

what's the point to assign a value to a property inside a class? each time
the page reloads the class loose that value...

to have a "Global" variable we need to use session (if we want that value to
expire after the session.timeout) or profiles (to save the value forever -
saving it in a DB)... right? ...off course, and cookies, but I'm trying to
build a website cookieless :)

--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:%[email protected]...
 
K

Karl Seguin [MVP]

The purpose of a class isn't to help with state - it's to help model your
business domain. Whether it's short lived or not, it can accomplish this
task very well.

In one of my earlier posts, I suggest that you implement some type of
factory to recreate the instance from the session. I still think that's the
right way to go.

public static UserBoxes
{
get
{
if (Session["Something"] != null)
{
return new Boxes();
}
return (Boxex)Session["Something"];
}
}

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/


Bruno Alexandre said:
so why using Class's in ASP.NET?

what's the point to assign a value to a property inside a class? each time
the page reloads the class loose that value...

to have a "Global" variable we need to use session (if we want that value
to expire after the session.timeout) or profiles (to save the value
forever - saving it in a DB)... right? ...off course, and cookies, but I'm
trying to build a website cookieless :)

--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:%[email protected]...
 
B

Bruno Alexandre

got it :)

Thank you for the help, and... nice Blog :)

--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:[email protected]...
The purpose of a class isn't to help with state - it's to help model your
business domain. Whether it's short lived or not, it can accomplish this
task very well.

In one of my earlier posts, I suggest that you implement some type of
factory to recreate the instance from the session. I still think that's
the right way to go.

public static UserBoxes
{
get
{
if (Session["Something"] != null)
{
return new Boxes();
}
return (Boxex)Session["Something"];
}
}

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/


Bruno Alexandre said:
so why using Class's in ASP.NET?

what's the point to assign a value to a property inside a class? each
time the page reloads the class loose that value...

to have a "Global" variable we need to use session (if we want that value
to expire after the session.timeout) or profiles (to save the value
forever - saving it in a DB)... right? ...off course, and cookies, but
I'm trying to build a website cookieless :)

--


Thank you in Advance.

Bruno Alexandre
(a Portuguese in Denmark)

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> escreveu na mensagem news:%[email protected]...
 

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

Members online

No members online now.

Forum statistics

Threads
473,815
Messages
2,569,705
Members
45,494
Latest member
KandyFrank

Latest Threads

Top