468,272 Members | 2,146 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

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

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(Co okieName)
If x Is Nothing Then
Return NothingReturnValue
Else
Return System.Web.HttpContext.Current.Request.Cookies(Coo kieName).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(Coo kieName).Value never return a value that was set earlier (but the instance method does)

Thank you for your help

Earl
Nov 18 '05 #1
1 3355
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" <Ea*********@discussions.microsoft.com> wrote in message
news:6D**********************************@microsof t.com...
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(Co okieName) If x Is Nothing Then
Return NothingReturnValue
Else
Return System.Web.HttpContext.Current.Request.Cookies(Coo kieName).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(Coo kieName).Value never
return a value that was set earlier (but the instance method does)
Thank you for your help

Earl

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Powell | last post: by
3 posts views Thread by M Wells | last post: by
6 posts views Thread by Mark | last post: by
reply views Thread by tshad | last post: by
5 posts views Thread by Dinsdale | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.