keyword this

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

    keyword this

    In this code :

    public abstract class DCMenuAgrupado

    {

    Object x = this;

    }



    Why I can t use keyword 'this' ?

    compile error : Keyword this is not available in the current context




    Thanks.




  • Peter van der Goes

    #2
    Re: keyword this


    "PauloFor" <paulofor@uol.c om.br> wrote in message
    news:eVGUWAmYFH A.3616@TK2MSFTN GP15.phx.gbl...[color=blue]
    > In this code :
    >
    > public abstract class DCMenuAgrupado
    >
    > {
    >
    > Object x = this;
    >
    > }
    >
    >
    >
    > Why I can t use keyword 'this' ?
    >
    > compile error : Keyword this is not available in the current context
    >
    >[/color]
    The this keyword is a reference (pointer) to the current object. The lone
    executable statement you cite in your class definition is not within a
    method, which is probably causing the problem. Another potential sticking
    point is that your class is abstract, and thus, cannot be instantiated. How
    can there be a current object?
    See what happens if you try something like this (no pun intended):

    public class MyClass
    {
    public void myMethod()
    {
    // put you code here
    }
    }

    --
    Peter [MVP Visual Developer]
    Jack of all trades, master of none.


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: keyword this

      Peter van der Goes <p_vandergoes@t oadstool.u> wrote:[color=blue]
      > The this keyword is a reference (pointer) to the current object. The lone
      > executable statement you cite in your class definition is not within a
      > method, which is probably causing the problem.[/color]

      Indeed. You can't use "this" for an instance variable initialiser.
      [color=blue]
      > Another potential sticking
      > point is that your class is abstract, and thus, cannot be instantiated. How
      > can there be a current object?[/color]

      By there being an instance of a derived class.


      However, you *can* use "this" in a constructor, achieving much the same
      result as an instance variable initialiser. In other words, from:

      public abstract class DCMenuAgrupado
      {
      Object x = this;
      }

      to

      public abstract class DCMenuAgrupado
      {
      Object x;

      public DCMenuAgrupado( )
      {
      x = this;
      }
      }

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Bill Butler

        #4
        Re: keyword this


        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        <snip>[color=blue]
        > public abstract class DCMenuAgrupado
        > {
        > Object x;
        >
        > public DCMenuAgrupado( )
        > {
        > x = this;
        > }
        > }
        >[/color]


        I am a bit confused.
        Other then providing an alias for "this", what is the point?

        So....Why would you want to do this?

        Bill


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: keyword this

          Bill Butler <qwerty@asdf.co m> wrote:[color=blue]
          > <snip>[color=green]
          > > public abstract class DCMenuAgrupado
          > > {
          > > Object x;
          > >
          > > public DCMenuAgrupado( )
          > > {
          > > x = this;
          > > }
          > > }[/color]
          >
          > I am a bit confused.
          > Other then providing an alias for "this", what is the point?
          >
          > So....Why would you want to do this?[/color]

          One example might be for a SyncRoot property, which defaulted to
          "this" but which could be set to something else.

          That's the only thing I can immediately think of, but we'd have to ask
          the OP :)

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...