473,804 Members | 3,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Aug 17 '08 #1
3 2082
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
_____
http://www.guffa.com
Aug 17 '08 #2
ChrisW <ch*******@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 - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 17 '08 #3
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
Aug 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16716
by: Murat Tasan | last post by:
i have an inner class and an outer class, both of which have an identically named member. how from the inner class can i reference the outer member? (the example code should illustrate this problem). class Outer { int x; class Inner { int x;
3
44535
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult to read (versus the format I have used for years). I have tested the queries below using the SQL Profiler, and both have identical costs. Is there any advantage of one format over the other?
8
4976
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and also display their corresponding entries in arb, but if there is NO entry in arb I still want it to show up as NULL or something, so that I can get the attention that there IS no language associated with that article.
3
4940
by: trialproduct2004 | last post by:
hi all i am having application which is using two classes. in one class i am creating instance of another class and calling method of that class. what i want is from inner class's method i want to invoke method of outer class. can i use delegates to do this. any help will be appreciated. thanks in advance.
9
1863
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added to the order */ }
2
2942
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
1
1546
by: Edward C. Jones | last post by:
#! /usr/bin/env python """ When I run the following program I get the error message: UnboundLocalError: local variable 'x' referenced before assignment Can "inner" change the value of a variable defined in "outer"? Where is this explained in the docs? """ def outer():
5
2432
by: Martijn Mulder | last post by:
A construction like this: class Outer { class Inner:Outer { } } compiles without problem but does it introduce infinity?
9
4694
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10594
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.