Connecting Tech Pros Worldwide Forums | Help | Site Map

need Help on a Class livetime under ASP.NET

Bruno Alexandre
Guest
 
Posts: n/a
#1: Apr 6 '06
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)



Karl Seguin [MVP]
Guest
 
Posts: n/a
#2: Apr 6 '06

re: need Help on a Class livetime under ASP.NET


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
--
http://www.openmymind.net/



"Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
news:ePwC1mVWGHA.1900@TK2MSFTNGP04.phx.gbl...[color=blue]
> 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)
>
>[/color]


Bruno Alexandre
Guest
 
Posts: n/a
#3: Apr 6 '06

re: need Help on a Class livetime under ASP.NET


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:%23perGFXWGHA.1348@TK2MSFTNGP05.phx.gbl...[color=blue]
> 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
> --
> http://www.openmymind.net/
>
>
>
> "Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
> news:ePwC1mVWGHA.1900@TK2MSFTNGP04.phx.gbl...[color=green]
>> 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)
>>
>>[/color]
>
>[/color]


Karl Seguin [MVP]
Guest
 
Posts: n/a
#4: Apr 6 '06

re: need Help on a Class livetime under ASP.NET


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" <bruno.pub@filtrarte.com> wrote in message
news:OOwMPXYWGHA.4652@TK2MSFTNGP04.phx.gbl...[color=blue]
>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:%23perGFXWGHA.1348@TK2MSFTNGP05.phx.gbl...[color=green]
>> 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
>> --
>> http://www.openmymind.net/
>>
>>
>>
>> "Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
>> news:ePwC1mVWGHA.1900@TK2MSFTNGP04.phx.gbl...[color=darkred]
>>> 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)
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Bruno Alexandre
Guest
 
Posts: n/a
#5: Apr 7 '06

re: need Help on a Class livetime under ASP.NET


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)


Karl Seguin [MVP]
Guest
 
Posts: n/a
#6: Apr 7 '06

re: need Help on a Class livetime under ASP.NET


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

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



"Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
news:uIUw1KiWGHA.1084@TK2MSFTNGP04.phx.gbl...[color=blue]
> 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)
>
>[/color]


Bruno Alexandre
Guest
 
Posts: n/a
#7: Apr 7 '06

re: need Help on a Class livetime under ASP.NET


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:%23PHfcnjWGHA.4452@TK2MSFTNGP04.phx.gbl...[color=blue]
> 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
>
> --
> http://www.openmymind.net/
>
>
>
> "Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
> news:uIUw1KiWGHA.1084@TK2MSFTNGP04.phx.gbl...[color=green]
>> 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)
>>
>>[/color]
>
>[/color]


Karl Seguin [MVP]
Guest
 
Posts: n/a
#8: Apr 7 '06

re: need Help on a Class livetime under ASP.NET


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" <bruno.pub@filtrarte.com> wrote in message
news:Ou9wG3jWGHA.2064@TK2MSFTNGP03.phx.gbl...[color=blue]
> 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:%23PHfcnjWGHA.4452@TK2MSFTNGP04.phx.gbl...[color=green]
>> 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
>>
>> --
>> http://www.openmymind.net/
>>
>>
>>
>> "Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
>> news:uIUw1KiWGHA.1084@TK2MSFTNGP04.phx.gbl...[color=darkred]
>>> 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)
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Bruno Alexandre
Guest
 
Posts: n/a
#9: Apr 7 '06

re: need Help on a Class livetime under ASP.NET


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:OQoPRdkWGHA.4960@TK2MSFTNGP05.phx.gbl...[color=blue]
> 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" <bruno.pub@filtrarte.com> wrote in message
> news:Ou9wG3jWGHA.2064@TK2MSFTNGP03.phx.gbl...[color=green]
>> 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:%23PHfcnjWGHA.4452@TK2MSFTNGP04.phx.gbl...[color=darkred]
>>> 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
>>>
>>> --
>>> http://www.openmymind.net/
>>>
>>>
>>>
>>> "Bruno Alexandre" <bruno.pub@filtrarte.com> wrote in message
>>> news:uIUw1KiWGHA.1084@TK2MSFTNGP04.phx.gbl...
>>>> 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)
>>>>
>>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Closed Thread