Changing Response.Cookie from Instance to Shared (Static) Problem

G

Guest

When I write inline code on a aspx page using Page.Response.Cookies() to read a cookie, everything works fine, as in the following code.

Dim x As Object = Request.Cookies("thing")
If x Is Nothing Then
CurrentValue.Text = "No Cookie Defined"
Else
CurrentValue.Text = Request.Cookies("thing").Value
End If

However, I wanted to move the code to a shared method so I do not have access to the instance Page class any more so I used the shared method from System.Web.HttpContext.Current.Request.Cookies(). This, however, did not yeald the same results as the oringonal code (Aka: did not work). See the following code

Private Shared Function ReadCookieOr(ByVal CookieName As String, ByVal NothingReturnValue As String) As String
Dim x As Object = System.Web.HttpContext.Current.Response.Cookies(CookieName)
If x Is Nothing Then
Return NothingReturnValue
Else
Return System.Web.HttpContext.Current.Request.Cookies(CookieName).Value
End If
End Function

Here are my issues:
1. How do I determine if the Shared(or static) Cookies method contains a value, like I do in the inline code (using: If x Is Nothing)
2. If I do need assign a reference of this class to an object, how do I cast that object back to origonal class (I guess in VB.net it would use ctype...)
3. In the above example, why does System.Web.HttpContext.Current.Request.Cookies(CookieName).Value never return a value that was set earlier (but the instance method does)

Thank you for your help

Earl
 
M

Marina

Cookies is a property of the Request or Response object, not a method.

1) I don't understand what you mean. 'If x Is Nothing', will work in either
case
2) You should not declare x as Object, it should be an HttpCookie. Always
use 'Options Strict On'.
3) Because you set 'x' to a cookie from the Response, not Request. Where as
in the inline code, you always use Request.

Earl Teigrob said:
When I write inline code on a aspx page using Page.Response.Cookies() to
read a cookie, everything works fine, as in the following code.
Dim x As Object = Request.Cookies("thing")
If x Is Nothing Then
CurrentValue.Text = "No Cookie Defined"
Else
CurrentValue.Text = Request.Cookies("thing").Value
End If

However, I wanted to move the code to a shared method so I do not have
access to the instance Page class any more so I used the shared method from
System.Web.HttpContext.Current.Request.Cookies(). This, however, did not
yeald the same results as the oringonal code (Aka: did not work). See the
following code
Private Shared Function ReadCookieOr(ByVal CookieName As String, ByVal
NothingReturnValue As String) As String
Dim x As Object = System.Web.HttpContext.Current.Response.Cookies(CookieName)
If x Is Nothing Then
Return NothingReturnValue
Else
Return System.Web.HttpContext.Current.Request.Cookies(CookieName).Value
End If
End Function

Here are my issues:
1. How do I determine if the Shared(or static) Cookies method contains a
value, like I do in the inline code (using: If x Is Nothing)
2. If I do need assign a reference of this class to an object, how do I
cast that object back to origonal class (I guess in VB.net it would use
ctype...)
3. In the above example, why does
System.Web.HttpContext.Current.Request.Cookies(CookieName).Value never
return a value that was set earlier (but the instance method does)
 

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

Top