I have a class whereby a method calls a nested class. I want to access the parent class properties from within the nested class.
public class ParentClass
{
private x;
private y;
private z;
something.something = new ChildClass();
public class ChildClass
{
// need to get x, y and z;
}
}
How do I access x,y and z from within the child class? Something to do with referencing the parent class, but how?
Use the this keyword to pass a reference to 'yourself' to the constructor of the ChildClass.
public class ParentClass
{
public X;
public Y;
public Z;
// give the ChildClass instance a reference to this ParentClass instance
ChildClass cc = new ChildClass(this);
public class ChildClass
{
private ParentClass _pc;
public ChildClass(ParentClass pc) {
_pc = pc;
}
// need to get X, Y and Z;
public void GetValues() {
myX = _pc.X
...
}
}
}
See http://www.codeproject.com/KB/cs/nested_csclasses.aspx for a detailed tutorial on using nested classes in C#. I think you're looking for something like:
class OuterClass
{
public int y = 100;
public class NestedClass
{
public static void abc()
{
OuterClass oc = new OuterClass();
System.Console.WriteLine(oc.y);
}
}
}
So, in order to access the fields of the outer class, you need an instance of the outer class available to the inner class.
Keep in mind that you can access static fields from the inner class without an instance of the outer class around:
class OuterClass
{
public static int y = 100;
public class NestedClass
{
public static void abc()
{
System.Console.WriteLine(OuterClass.y);
}
}
}
You need to pass in a reference to the parent class instance, for instance in the constructor of ChildClass. Of course you can access fields of ParentClass if those are static.
Note: If you have ever done Java, C# only supports the notion of the "static" inner class.
Well, on the constructor of your nested class pass in a reference to the outer class.
That way you can access the parent class properties from within the nested class.
Also, it's worth noting that static properties from the parent class, are available to you.
http://en.csharp-online.net/Nested_Classes
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Application {
class OuterClass {
int someProperty = 10;
class NestedClass {
OuterClass reference;
public NestedClass( OuterClass r ) {
reference = r;
}
public void DoSomething( ) {
Console.Write( reference.someProperty );
}
}
public OuterClass( ) {
NestedClass nc = new NestedClass( this );
nc.DoSomething( );
}
}
class Test {
public static void Main( string[] args ) {
OuterClass oc = new OuterClass( );
}
}
}
Related
I want to access the member variables inside a class MainClass, from inside a class defined in that MainClass. Can i do that?
public class MainClass
{
public int x = 1;
public class Class1
{
public void class1Function()
{
this.x = 4; //somehow access the x of instance of MainClass
}
}
public static class Class2
{
public static void class2Function()
{
this.x = 5; //also can i do this from a static function?
}
}
public void mainFunction()
{
System.Console.WriteLine(this.x); //this works just fine, obviously
}
}
Class1 and Class2 cannot access x from MainClass because an instance of a nested class isn't associated with an instance of the outer class. (Corrected based on Joe Sewell's hint)
You could inherit MainClass like this: public class Class1 : MainClass. Then you can access this.x.
Accessing a mutable variable from a static context, which is the case for Class2 is not possible. If you want to use x in class2Function you could pass it and return the modified version that you need (Closure of Operations).
I have created an object that holds 4 properties, and will get created and used in Class A. However, I need to call some of the properties in 2 other classes (Class B and Class C). I'm not able to do this, and I'm confident that I am missing something very simple. Here is my object creation class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
public class ConfigItems
{
public static string W { get; set; }
public static string X { get; set; }
public static int Y { get; set; }
public static Int64 Z { get; set; }
public ConfigItems(string w, string x, int y, Int64 z) {
W= w;
X= x;
Y= y;
Z= z;
}
}
}
In my Main class, this is how I'm creating the object, which works just fine as long as I remain in the Main class:
namespace Test
{
public class Main
{
ConfigItems mainSetup = new ConfigItems(w, x, y, z);
console.writeline(mainSetup.x);
}
}
In the Main class when I create the object, I can keep calling it. When I move to a new class and try to call the same object, I cannot. It is out of scope, and I'm not certain as to why.
namespace SomeClass
{
public class StuffHere
{
Console.Writeline(mainSetup.x);
}
}
mainSetup.x in the above will not return anything, because it is out of scope. I have tried using both static and non-static properties in the object creation lass (ConfigItems) but I still can't call mainSetup.x outside of the Main class
You have defined your properties as static, therefore they are not instance members, but members of the class. Remove the static keyword.
public string X { get; set; }
Your class is not static! so you can't access class's property directly.
You have two way:
Make your class static.
public static class ConfigItems {
}
Pass the class context from initialer class (A) to other classes (B or C).
Class A:
ConfigItems mainSetup = new ConfigItems(w, x, y, z);
SecondClass b = new SecondClass(mainSetup);
Class B or C:
public class SecondClass {
public SecondClass (ConfigItems config) {
// You have the ConfigItems class here and you can do anything with it
}
}
you can access them directly using CalssName
ConfigItems.X;
namespace MyProgram
{
public class ParentClass
{
}
public class childClass : ParentClass
{
public int Normal()
{
return 1;
}
}
}
SomeWhere else:
ParentClass parentc = new ParentClass();
parentc.Normal(); //<-- I can't call the function normal!
I need help, to use it.
Parent does not have a method Normal, so you can't call it. By basic logic, a parent does not inherit its child's methods; it's the other way around.
Move the normal method to the parent class:
public class ParentClass
{
public int Normal()
{
return 1;
}
}
public class childClass : ParentClass
{
}
You have to Create Refferance of Child Class to Acess the Normal(); function .
childClass obj=new childClass();
obj.Normal();
Try something like this;
class ParentClass
{
public int Normal()
{
return 1;
}
}
// Derived class
class ChildClass: ParentClass
{
public int someMethod()
{
return Normal();
}
}
You need to cast the object down.
You do it when you need to apply a child's function to a parent object.
In order to do that, you need to declare you want to cast the object later by using the child constructor when creating the parent, like so:
class foo{}
class goo:foo{ public void DoStuff(){} }
/*.... In the main program ..... */
foo a = new goo(); // Declaring the a, which is of type foo, might be used as a goo later on.
And for useage, cast it down.
((goo)a).DoStuff();
Hope it helps :)
You cannot call a child class method from parent. But can call parent class method from child.
namespace MyProgram
{
public class ParentClass
{
public int Normal()
{
return 1;
}
}
public class childClass : ParentClass
{
Normal(); // which calls the method in Base class(ParentClass)
//base.Normal(); //or this one in which base tells that the method is in base class
}
}
I have error
Cannot access a non-static member of outer type 'Project.Neuro' via
nested type 'Project.Neuro.Net'
with code like this (simplified):
class Neuro
{
public class Net
{
public void SomeMethod()
{
int x = OtherMethod(); // error is here
}
}
public int OtherMethod() // its outside Neuro.Net class
{
return 123;
}
}
I can move problematic method to Neuro.Net class, but I need this method outside.
Im kind of objective programming newbie.
Thanks in advance.
The problem is that nested classes are not derived classes, so the methods in the outer class are not inherited.
Some options are
Make the method static:
class Neuro
{
public class Net
{
public void SomeMethod()
{
int x = Neuro.OtherMethod();
}
}
public static int OtherMethod()
{
return 123;
}
}
Use inheritance instead of nesting classes:
public class Neuro // Neuro has to be public in order to have a public class inherit from it.
{
public static int OtherMethod()
{
return 123;
}
}
public class Net : Neuro
{
public void SomeMethod()
{
int x = OtherMethod();
}
}
Create an instance of Neuro:
class Neuro
{
public class Net
{
public void SomeMethod()
{
Neuro n = new Neuro();
int x = n.OtherMethod();
}
}
public int OtherMethod()
{
return 123;
}
}
you need to instantiate an object of type Neuro somewhere in your code and call OtherMethod on it, since OtherMethod is not a static method. Whether you create this object inside of SomeMethod, or pass it as an argument to it is up to you. Something like:
// somewhere in the code
var neuroObject = new Neuro();
// inside SomeMethod()
int x = neuroObject.OtherMethod();
alternatively, you can make OtherMethod static, which will allow you to call it from SomeMethod as you currently are.
Even though class is nested within another class, it is still not obvious which instance of outer class talks to which instance of inner class. I could create an instance of inner class and pass it to the another instance of outer class.
Therefore, you need specific instance to call this OtherMethod().
You can pass the instance on creation:
class Neuro
{
public class Net
{
private Neuro _parent;
public Net(Neuro parent)
{
_parent = parent;
}
public void SomeMethod()
{
_parent.OtherMethod();
}
}
public int OtherMethod()
{
return 123;
}
}
I think making an instance of outer class in inner class is not a good option because you may executing business logic on outer class constructor. Making static methods or properties is better option. If you insist making an instance of outer class than you should add another parameter to outer class contructor that not to execute business logic.
Here is my code (just a snippet to expose the problem) :
public class A
{
class B
{
//private class
}
public int nb;
}
Im tired but why can't I access to "nb" in my private class ?
You're gonna need an instance of A in order to access the instance member nb:
public class A
{
class B
{
public B()
{
A a = new A();
int nb = a.nb;
}
}
public int nb;
}
It's possible in java but not in C#.
You need to pass an instance of A to B.
In C# an 'outer' class is just a 'namespace' to the inner class. So the outer class is not being instantiated.
You need to pass an instance of A to B, like so:
public class A
{
class B
{
private A _outerClass;
public B(A outerClass)
{
_outerClass = outerClass;
// Then you can access nb thus:
_outerClass.nb;
}
}
public int nb;
}