Outer and inner classes: error

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

    Outer and inner classes: error

    Hiya,

    So I have a class that creates threads within it. These threads are a
    class underneath the parent class. I want to access values in the
    parent class from the threads while they run. Yet this gives me an
    error: cannot access a non-static member of outer type 'Parent.Class'
    via nested type 'Parent.Class.T hread'.

    So how do I access values in the parent class?

    Thanks!!

    ChrisW
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Outer and inner classes: error

    ChrisW wrote:
    Hiya,
    >
    So I have a class that creates threads within it. These threads are a
    class underneath the parent class. I want to access values in the
    parent class from the threads while they run. Yet this gives me an
    error: cannot access a non-static member of outer type 'Parent.Class'
    via nested type 'Parent.Class.T hread'.
    >
    So how do I access values in the parent class?
    >
    Thanks!!
    >
    ChrisW
    If you had static variables in the class, i would work the way that you
    described, so what you want to access is a variable in an instance of
    the parent class.

    You need a reference to the instance of the parent class in the inner
    class to be able to access the variables in the instance.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Outer and inner classes: error

      ChrisW <chriswo2k@gmai l.comwrote:
      So I have a class that creates threads within it. These threads are a
      class underneath the parent class. I want to access values in the
      parent class from the threads while they run. Yet this gives me an
      error: cannot access a non-static member of outer type 'Parent.Class'
      via nested type 'Parent.Class.T hread'.
      >
      So how do I access values in the parent class?
      I suggest we leave threading out of it, as threads aren't classes.

      Now, if you want to tell a nested type about an instance of its outer
      type, you need to provide it with that information, just as if it were
      any other type. Unless inner classes in Java, there's no implicit outer
      class instance involved. Here's an example:

      using System;

      class Outer
      {
      private int x;

      class Nested
      {
      Outer outer;

      internal Nested(Outer outer)
      {
      this.outer = outer;
      }

      internal void Foo()
      {
      Console.WriteLi ne("outer.x = {0}", outer.x);
      }
      }

      static void Main()
      {
      Outer o = new Outer();
      o.x = 10;

      Nested nested = new Nested(o);
      nested.Foo();
      }
      }


      This also demonstrates that a nested type has access to their outer
      type's private members.

      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com

      Comment

      • sudeshsinghchandel@gmail.com

        #4
        Re: Outer and inner classes: error

        On Aug 18, 12:24 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        ChrisW <chrisw...@gmai l.comwrote:
        So I have aclassthat creates threads within it. These threads are a
        classunderneath the parentclass. I want to access values in the
        parentclassfrom the threads while they run. Yet this gives me an
        error:cannotacc ess a non-static member of outer type 'Parent.Class'
        via nested type 'Parent.Class.T hread'.
        >
        So how do I access values in the parentclass?
        >
        I suggest we leave threading out of it, as threads aren't classes.
        >
        Now, if you want to tell a nested type about an instance of its outer
        type, you need to provide it with that information, just as if it were
        any other type. Unless inner classes inJava, there's no implicit outerclassinsta nce involved. Here's an example:
        >
        using System;
        >
        classOuter
        {
           privateint x;
        >
           classNested
            {
                Outer outer;
        >
                internal Nested(Outer outer)
                {
                    this.outer = outer;
                }
        >
                internal void Foo()
                {
                    Console.WriteLi ne("outer.x = {0}", outer.x);
                }
            }
        >
            static void Main()
            {
                Outer o = new Outer();
                o.x = 10;
        >
                Nested nested = new Nested(o);
                nested.Foo();
            }
        >
        }
        >
        This also demonstrates that a nested type has access to their outer
        type'sprivateme mbers.
        >
        --
        Jon Skeet - <sk...@pobox.co m>
        Web site:http://www.pobox.com/~skeet 
        Blog:http://www.msmvps.com/jon.skeet
        C# in Depth:http://csharpindepth.com
        I think there is something else involved in your query.
        Can U provide the snapshot of the code to answer the query properly

        Comment

        Working...