468,291 Members | 1,427 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Cookie in class

When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub

Nov 18 '05 #1
6 1246
No it is not with the imports, it is connected with the context within the
current sub is executing
i whould suggest you to pass the current HttpResponse as a parametter to the
sub

Regards
Martin

"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub

Nov 18 '05 #2
The Response object is a member of the HttpContext class, which is a member
of the Page class. That is why you can access it directly in your Page code.
Every HTTP Request causes ASP.Net to create an HttpContext class, which is
passed to the HttpHandler (in this case, an ASPX Page class). It contains
all the elements vital to reading the Request, forming and sending the
Response, and access to the Server. Fortunately, when a class is being used
by an HttpHandler (such as a Page), it can access the entire contents of the
HttpContext, by calling upon System.Web.HttpContext.Current. Among these
static members is the Response. So, in your class, you can refer to
System.Web.HttpContext.Current.Response.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub

Nov 18 '05 #3
When your code is not in a Page, you can use the System.web.httpContext
class to gain access to the current http request...

Patrice

--

"hansiman" <ha***@hotmail.com> a écrit dans le message de
news:d3********************************@4ax.com...
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub

Nov 18 '05 #4
The best way to handle this is for your class to expose a function that returns a cookie to the calling sub/function, then your main page actually sets the cookie. This is the object orientated approach.

"hansiman" <ha***@hotmail.com> wrote in message news:d3********************************@4ax.com...
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten


Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub

Nov 18 '05 #5
"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com...
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub


"Response" is short for System.Web.UI.Page.Response. On an aspx page, you
can refer to it as "Response" because the page derives from
System.Web.UI.Page.

Your new class does not derive from System.Web.UI.Page (and it shouldn't),
so you need to find the response object you want. If your sub was called
from a page, you will find the response object at
System.Web.HttpContext.Current.Response.
--
John Saunders
johnwsaundersiii at hotmail
Nov 18 '05 #6
Aha.... Thanks :)

On Fri, 11 Jun 2004 09:16:56 -0400, "John Saunders"
<jo**************@notcoldmail.com> wrote:
"hansiman" <ha***@hotmail.com> wrote in message
news:d3********************************@4ax.com.. .
When I use the sub code below directly in the code behind page it
works. However, if I move the code into a sub and place the sub in a
class file I get a building error "Name 'Response' is not declared."

I figure it has to do with the imports .... but I have tried both
system.web.ui and a couple of others...

Please help.

/morten
Public Sub addCookie(ByVal sName As String, ByVal sValue As String,
ByVal dExpiry As DateTime)
Dim cookie As HttpCookie = New HttpCookie(sName)
cookie.Value = sValue
cookie.Expires = dExpiry
Response.Cookies.Add(cookie)
End Sub


"Response" is short for System.Web.UI.Page.Response. On an aspx page, you
can refer to it as "Response" because the page derives from
System.Web.UI.Page.

Your new class does not derive from System.Web.UI.Page (and it shouldn't),
so you need to find the response object you want. If your sub was called
from a page, you will find the response object at
System.Web.HttpContext.Current.Response.


Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Phil Powell | last post: by
1 post views Thread by RCHENG | last post: by
3 posts views Thread by Karsten Grombach | last post: by
6 posts views Thread by Jason Collins | last post: by
2 posts views Thread by Shawn Berg | 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.