Accessing application variable from static method

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

    #1

    Accessing application variable from static method

    Hi All,

    Reformulating a problem from a previous post:

    How can I access an application variable from a static method?
    When I try:
    string myString = Application{"th isString"].ToString;

    whithi a static method, I get the message:
    CS01020 "In order to use a non-static field, method, or property, you
    must first create an object instance.

    How can I solve this?

    Thanks

    Dominique
    PS: Anybody knows of a good C# mailing list ?


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Accessing application variable from static method

    DDE,

    The reason this fails is because Application is really a property
    exposed by the page object. In order to use it in a static method, you will
    have to pass it into the static method (taking a type of
    HttpApplication State), or you can pass the Page object (which exposes the
    Application property), or you can use the static Current property on the
    HttpContext class, which will expose an Application property which you can
    use to get to the application.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "DDE" <dde@iaireinfo. com> wrote in message
    news:%23Z3nQqRf DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi All,
    >
    > Reformulating a problem from a previous post:
    >
    > How can I access an application variable from a static method?
    > When I try:
    > string myString = Application{"th isString"].ToString;
    >
    > whithi a static method, I get the message:
    > CS01020 "In order to use a non-static field, method, or property, you
    > must first create an object instance.
    >
    > How can I solve this?
    >
    > Thanks
    >
    > Dominique
    > PS: Anybody knows of a good C# mailing list ?
    >
    >[/color]


    Comment

    • DDE

      #3
      Re: Accessing application variable from static method

      Hi,

      thanks for your answer. It is what I suspected. In this case I am working in
      a Web Service and not a web page, what should be the type to use to pass it
      to my method?

      Thanks
      Dominique


      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:OecdyzRfDH A.3528@tk2msftn gp13.phx.gbl...[color=blue]
      > DDE,
      >
      > The reason this fails is because Application is really a property
      > exposed by the page object. In order to use it in a static method, you[/color]
      will[color=blue]
      > have to pass it into the static method (taking a type of
      > HttpApplication State), or you can pass the Page object (which exposes the
      > Application property), or you can use the static Current property on the
      > HttpContext class, which will expose an Application property which you can
      > use to get to the application.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "DDE" <dde@iaireinfo. com> wrote in message
      > news:%23Z3nQqRf DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > Hi All,
      > >
      > > Reformulating a problem from a previous post:
      > >
      > > How can I access an application variable from a static method?
      > > When I try:
      > > string myString = Application{"th isString"].ToString;
      > >
      > > whithi a static method, I get the message:
      > > CS01020 "In order to use a non-static field, method, or property,[/color][/color]
      you[color=blue][color=green]
      > > must first create an object instance.
      > >
      > > How can I solve this?
      > >
      > > Thanks
      > >
      > > Dominique
      > > PS: Anybody knows of a good C# mailing list ?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Accessing application variable from static method

        Dominique,

        You can use the third option I gave to you, which would be to use the
        Application property exposed by the HttpContext instance returned by the
        static Current method on the HttpContext class.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - nicholas.paldin o@exisconsultin g.com

        "DDE" <dde@iaireinfo. com> wrote in message
        news:uOxklLSfDH A.1648@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hi,
        >
        > thanks for your answer. It is what I suspected. In this case I am working[/color]
        in[color=blue]
        > a Web Service and not a web page, what should be the type to use to pass[/color]
        it[color=blue]
        > to my method?
        >
        > Thanks
        > Dominique
        >
        >
        > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>[/color]
        wrote[color=blue]
        > in message news:OecdyzRfDH A.3528@tk2msftn gp13.phx.gbl...[color=green]
        > > DDE,
        > >
        > > The reason this fails is because Application is really a property
        > > exposed by the page object. In order to use it in a static method, you[/color]
        > will[color=green]
        > > have to pass it into the static method (taking a type of
        > > HttpApplication State), or you can pass the Page object (which exposes[/color][/color]
        the[color=blue][color=green]
        > > Application property), or you can use the static Current property on the
        > > HttpContext class, which will expose an Application property which you[/color][/color]
        can[color=blue][color=green]
        > > use to get to the application.
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - nicholas.paldin o@exisconsultin g.com
        > >
        > > "DDE" <dde@iaireinfo. com> wrote in message
        > > news:%23Z3nQqRf DHA.2352@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
        > > > Hi All,
        > > >
        > > > Reformulating a problem from a previous post:
        > > >
        > > > How can I access an application variable from a static method?
        > > > When I try:
        > > > string myString = Application{"th isString"].ToString;
        > > >
        > > > whithi a static method, I get the message:
        > > > CS01020 "In order to use a non-static field, method, or property,[/color][/color]
        > you[color=green][color=darkred]
        > > > must first create an object instance.
        > > >
        > > > How can I solve this?
        > > >
        > > > Thanks
        > > >
        > > > Dominique
        > > > PS: Anybody knows of a good C# mailing list ?
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • DDE

          #5
          Re: Accessing application variable from static method

          Thanks.


          Comment

          Working...