Outer and inner classes: error

C

ChrisW

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.Thread'.

So how do I access values in the parent class?

Thanks!!

ChrisW
 
G

Göran Andersson

ChrisW said:
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.Thread'.

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.
 
J

Jon Skeet [C# MVP]

ChrisW said:
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.Thread'.

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.WriteLine("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.
 
S

sudeshsinghchandel

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 outerclassinstance involved. Here's an example:

using System;

classOuter
{
   privateint x;

   classNested
    {
        Outer outer;

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

        internal void Foo()
        {
            Console.WriteLine("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'sprivatemembers.

--
Jon Skeet - <[email protected]>
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
 

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