Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of using constants for the session keys, I'm using my own type-safe session object, which looks like this (sorry this is in C#, see below for a VB version):</p> <pre><code>public class MySession { // Private constructor (use MySession.Current to access the current instance). private MySession() {} // Gets the current session. public static MySession Current { get { MySession session = HttpContext.Current.Session["__MySession__"] as MySession; if (session == null) { session = new MySession(); HttpContext.Current.Session["__MySession__"] = session; } return session; } } // My session data goes here: public string MyString { get; set; }; public bool MyFlag { get; set; }; public int MyNumber { get; set; }; } </code></pre> <p>Whenever I need to read/write something to/from the session, I can use my typesafe session object like this:</p> <pre><code>string s = MySession.Current.MyString; s = "new value"; MySession.Current.MyString = s; </code></pre> <p>This solution results in several advantages:</p> <ul> <li>I have a typesafe Session (no more type-casts)</li> <li>I can document all session based data (by commenting the public properties in MySession)</li> <li>When adding a new element to the session, I don't have to search the solution to check if the same session-key was already used somewhere else.</li> </ul> <p><strong>Update:</strong> Here's a VB version (automatically converted from the C# version). Sorry, but I don't know VB and so I didn't know how to write the properties in VB:</p> <pre><code>Public Class MySession ' Private constructor (use MySession.Current to access the current instance). Private Sub New() End Sub ' Gets the current session. Public Shared ReadOnly Property Current() As MySession Get Dim session As MySession = TryCast(HttpContext.Current.Session("__MySession__"), MySession) If session = Nothing Then session = New MySession() HttpContext.Current.Session("__MySession__") = session End If Return session End Get End Property ' My session data goes here: Public MyString As String Public MyFlag As Boolean Public MyNumber As Integer End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload