private string m_myVar; protected virtual Page_Load(object sender, EventArgs e) { object result = Session["MySessionVar"]; if (result != null) this.m_my_Var = result.ToString(); }
Private Dim m_myVar As String Protected Overridable Sub Page_Load(ByVal sender As object, ByVal e As Eventargs) object result = Session("MySessionVar") If result <> Nothing Then Me.m_myVar = result.ToString() End If End SubIt doesn't happen very often, but things like Session and Cache can expire between the place where you first check to see if the variable is null and when you then try to use it. By assigning it to something local you avoid that problem (which can be very difficult to find since you're not expecting it).