Reference a session object

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.
 
S

Scott Allen

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.
 
L

Lars Dahlback

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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top