Im programming in C # .NET. I want to create a nested class that can access the members of the instance that created it, but I cannot figure out how to do this.
This is what I want to do:
Car x = new Car() x.color = "red"; x.Door frontDoor = new x.Door(); MessageBox.Show(frontDoor.GetColor()); // So I want the method GetColor of the class Front Door to be able to access the color field/property of the Car instance that created it.
How should I do it? I tried to nest the Door class inside the Car class, but it cannot access the members of the Car class this way. Do I need to get a car to inherit a door class or something else?
The easiest way is to return the Doorlink to the Carone that created it.
Door
Car
For instance:
class Car { public string color; public Door Door() { return new Door(this); } class Door { Car owner; Door(Car owner) { this.owner = owner; } string GetColor() { return owner.color; } } }
, .
CAR, CAR. CAR DOOR , setter DOOR.
, - . - "". . , , , , = (this.Colour); 'this' . , , . , , , , .
, Door, Car. # ( - ), , , .
: Door, Car as. private readonly. GetColor() , color Car.
private readonly
GetColor()
color
GetColor() get -only Car Door. ( , ), .
get
Car , , Car Door, Composition
:
public class Car { public string Color; } public class Door { public string T; public Car C; }
. , Car Door.
Door , Car GetColor() :
public class Car { private class Door { public Door(Func<Color> getColourFunc) { } } }
Source: https://habr.com/ru/post/1733434/More articles:URL rewrite help - url-rewritingSplitting Word documents into smaller ones - c #That Visual C ++ parameter / option / flag is analogous to -ansi -pedantic in g ++ - c ++iPhone Referring to Resources in Separate Directories - iphoneProblem with delegates in C #, forms and multiple solutions - c #How to create a class that can instantiate another class and access private members of the owner - inheritanceOTP S / KEY One-Time Password - Folded Output MD5 Digest - java-meHow to get unlimited UIScrollView - iphoneHelp creating an Enum extension helper - c #CSS3 support in mobile browsers - androidAll Articles