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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jake

    #1

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

    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 getCartCookieVa lue()
    {

    if (Request.Cookie s["tepcart"] != null)
    {
    using (CartServiceCli ent client = new CartServiceClie nt())
    {
    retval = client.getCartI d(Request.Cooki es["tepcart"].Value);
    client.Close();
    }
    }
    return retval;
    }

  • DH

    #2
    Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

    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 getCartCookieVa lue()
    {
    >
    if (Request.Cookie s["tepcart"] != null)
    {
    using (CartServiceCli ent client = new CartServiceClie nt())
    {
    retval = client.getCartI d(Request.Cooki es["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?

    Comment

    • jake

      #3
      Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

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

      On Nov 21, 3:44 pm, DH <I.dont.n...@mo re.spamwrote:
      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 getCartCookieVa lue()
      {
      >
         if (Request.Cookie s["tepcart"] != null)
         {
                 using (CartServiceCli ent client = new CartServiceClie nt())
                 {
                         retval = client.getCartI d(Request.Cooki es["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?

      Comment

      • jake

        #4
        Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

        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


        On Nov 21, 3:44 pm, DH <I.dont.n...@mo re.spamwrote:
        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 getCartCookieVa lue()
        {
        >
           if (Request.Cookie s["tepcart"] != null)
           {
                   using (CartServiceCli ent client = new CartServiceClie nt())
                   {
                           retval = client.getCartI d(Request.Cooki es["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?

        Comment

        • DH

          #5
          Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

          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
          >
          >
          On Nov 21, 3:44 pm, DH <I.dont.n...@mo re.spamwrote:
          >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 getCartCookieVa lue()
          >>{
          >> if (Request.Cookie s["tepcart"] != null)
          >> {
          >> using (CartServiceCli ent client = new CartServiceClie nt())
          >> {
          >> retval = client.getCartI d(Request.Cooki es["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.

          Comment

          • jake

            #6
            Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

            On Nov 21, 4:10 pm, DH <I.dont.n...@mo re.spamwrote:
            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
            >
            On Nov 21, 3:44 pm, DH <I.dont.n...@mo re.spamwrote:
            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 getCartCookieVa lue()
            >{
            >   if (Request.Cookie s["tepcart"] != null)
            >   {
            >           using (CartServiceCli ent client = new CartServiceClie nt())
            >           {
            >                   retval = client.getCartI d(Request.Cooki es["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.ge t" 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?

            Comment

            • Ignacio Machin ( .NET/ C# MVP )

              #7
              Re: An object reference is required for the non-static field, method,or property 'System.Web.UI. Page.Request.ge t'

              On Nov 21, 4:35 pm, jake <jakedim...@gma il.comwrote:
              On Nov 21, 4:10 pm, DH <I.dont.n...@mo re.spamwrote:
              >
              >
              >
              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
              >
              On Nov 21, 3:44 pm, DH <I.dont.n...@mo re.spamwrote:
              >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 getCartCookieVa lue()
              >>{
              >> if (Request.Cookie s["tepcart"] != null)
              >> {
              >> using (CartServiceCli ent client = new CartServiceClie nt())
              >> {
              >> retval = client.getCartI d(Request.Cooki es["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.ge t" 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.Cur rent.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

              Comment

              Working...