Reference a session object

  • Thread starter Thread starter Lars Dahlback
  • Start date Start date
L

Lars Dahlback

I have a VB .NET web application.

I have a structure of data I want to keep throughout the whole
session. Now I do it like below.

Structure SMyStruct
Dim number as Integer
......
End Structure


Private Sub Button_Go_Click(....) ....
Dim S As SMyStruct = Session("MyStruct")
S.number = 7
Session("MyStruct") = S
End Sub

This works, but not very efficient when the structure is big. The data
first gets copied from the Session then modified and then stored back
to the Session.

And I do NOT want to use the syntax
Session("MySessionObj").number = 7
for obvious reasons.

So is there any trick around that lets me get a reference to a session
variable so I can modify my structure members directly.
 
Hi Lars:

I'd strongly suggest you use a reference type. Define your type as a
Class instead of a Structure and you'll find the performance is better
and there will also be no need to copy the value back into the
session.

A Structure is a value type. The runtime has to be box and unbox the
value to move in and out of the session collection - there is no
avoiding it. Boxing is the process of turning a stack based value type
into a heap object.
 
Thank you Scott!

Also found a another way myself (below) but your way is the one.

Structure SMyStruct
Dim number as Integer
......
End Structure

Private Sub Button_Go_Click(....) ....
Go(Session("MyStruct"))
End Sub
Private Sub Go(ByRef S as SMyStruct)
S.number = 7
End Sub
 
Back
Top