An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Req

J

jake

I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.

My simplified code that generates the error is:

public static string getCartCookieValue()
{

if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}
 
D

DH

jake said:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.

My simplified code that generates the error is:

public static string getCartCookieValue()
{

if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}

where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
 
J

jake

I am sorry. Slip of the keyboard. I modified the message a bit.
Please refer to my latest version of the problem above.
jake

jake said:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
   if (Request.Cookies["tepcart"] != null)
   {
           using (CartServiceClient client = new CartServiceClient())
           {
                   retval = client.getCartId(Request.Cookies["tepcart"].Value);
                   client.Close();
           }
   }
   return retval;
}

where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
 
J

jake

If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.

The error is the one mention in the title of this message.
Thanks,
jake


jake said:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
   if (Request.Cookies["tepcart"] != null)
   {
           using (CartServiceClient client = new CartServiceClient())
           {
                   retval = client.getCartId(Request.Cookies["tepcart"].Value);
                   client.Close();
           }
   }
   return retval;
}

where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
 
D

DH

jake said:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.

The error is the one mention in the title of this message.
Thanks,
jake


jake said:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.
 
J

jake

jake said:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
The error is the one mention in the title of this message.
Thanks,
jake
jake wrote:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
   if (Request.Cookies["tepcart"] != null)
   {
           using (CartServiceClient client = new CartServiceClient())
           {
                   retval = client.getCartId(Request.Cookies["tepcart"].Value);
                   client.Close();
           }
   }
   return retval;
}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?

Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.

It is a .Net class. A simple web page actually. I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now. The
"Request.get" method is a non-static method that can only be in other
non-static methods. I guess the question now is: Is there another way
to get the cookies other than via the Request object?
 
I

Ignacio Machin ( .NET/ C# MVP )

jake said:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
The error is the one mention in the title of this message.
Thanks,
jake
jake wrote:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.

It is a .Net class. A simple web page actually. I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now. The
"Request.get" method is a non-static method that can only be in other
non-static methods. I guess the question now is: Is there another way
to get the cookies other than via the Request object?

Why you declare it as static in the first place?

In case you REALLY need it to be static you can use HttpContext:
HttpContext.Current.Request

Using HttpContext is very usefull when you have DLLs that are not
part of the web project. In those cases using HttpContext is the only
way to access the request
 
J

jake

jake wrote:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
The error is the one mention in the title of this message.
Thanks,
jake
jake wrote:
I think I may why this is happening but I am a little fuzzy as towhat
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
   if (Request.Cookies["tepcart"] != null)
   {
           using (CartServiceClient client = new CartServiceClient())
           {
                   retval = client.getCartId(Request.Cookies["tepcart"].Value);
                   client.Close();
           }
   }
   return retval;
}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really haveto.
It is a .Net class.  A simple web page actually.  I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now.  The
"Request.get" method is a non-static method that can only be in other
non-static methods.  I guess the question now is: Is there another way
to get the cookies other than via the Request object?

Why you declare it as static in the first place?

In case you REALLY need it to be static you can use HttpContext:
HttpContext.Current.Request

Using  HttpContext is very usefull when you have DLLs that are not
part of the web project. In those cases using HttpContext is the only
way to access the request

I don't actually have to have a static method had it not been a
preference of mine to create a class full of static utility functions
(methods) that I can call from anywhere in the project without
instantiating. But being somewhat new to web programming, I was
unaware of HttpContext. Serves me right for not doing my due
diligence before plunging into the unknown. At any rate, now that
 
J

jake

jake wrote:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
The error is the one mention in the title of this message.
Thanks,
jake
jake wrote:
I think I may why this is happening but I am a little fuzzy as towhat
the solution should be as I am somewhat new to all of this.
My simplified code that generates the error is:
public static string getCartCookieValue()
{
   if (Request.Cookies["tepcart"] != null)
   {
           using (CartServiceClient client = new CartServiceClient())
           {
                   retval = client.getCartId(Request.Cookies["tepcart"].Value);
                   client.Close();
           }
   }
   return retval;
}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really haveto.
It is a .Net class.  A simple web page actually.  I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now.  The
"Request.get" method is a non-static method that can only be in other
non-static methods.  I guess the question now is: Is there another way
to get the cookies other than via the Request object?

Why you declare it as static in the first place?

In case you REALLY need it to be static you can use HttpContext:
HttpContext.Current.Request

Using  HttpContext is very usefull when you have DLLs that are not
part of the web project. In those cases using HttpContext is the only
way to access the request

Actually Ignacio, I don't have to have a static method had it not been
a
preference of mine to create a class full of static utility functions
(methods) that I can call from anywhere in the project without
instantiating. But being somewhat new to web programming, I was
unaware of HttpContext. I was under the false impression that
System.Web.UI.Page.Request property that gets the HttpRequest object
for the requested page is the only way to get at a cookie. Serves me
right for not doing my due diligence before plunging into the
unknown. At any rate, now that I know about HttpContext, I may not
need to create a static method just for this purpose. Thanks again.
jake
 
F

Family Tree Mike

jake said:
I don't actually have to have a static method had it not been a
preference of mine to create a class full of static utility functions
(methods) that I can call from anywhere in the project without
instantiating. But being somewhat new to web programming, I was
unaware of HttpContext. Serves me right for not doing my due
diligence before plunging into the unknown. At any rate, now that


If you really want all methods in this class to be static, you should mark
the class itself as static. It will help prevent accidental missuse of the
class to create instances early.
 

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