public partial class EnterWidthAndHeight : Form { public EnterWidthAndHeight() { InitializeComponent(); } public int width_of_nonogram; public int height_of_nonogram; private void button1_Click(object sender, EventArgs e) { width_of_nonogram = int.Parse(WidthOfNonogram.ToString()); height_of_nonogram = int.Parse(HeightOfNonogram.ToString()); EnterColsAndRows enterColsAndRows = new EnterColsAndRows(width_of_nonogram,height_of_nonogram); enterColsAndRows.Show(); } } } public partial class EnterColsAndRows : Form { private int width_of_nonogram; private int height_of_nonogram; public EnterColsAndRows(int width,int height) { InitializeComponent(); width_of_nonogram = width; height_of_nonogram = height; } }
1 answer
To begin, let's talk about variables, fields, methods, etc.
Suppose we have a certain class:
public class MainClass { public string FieldOne; private string FieldTwo; public void Read() { Write("Hello!"); Console.WriteLine($"Приватное поле: {FieldTwo}"); Console.WriteLine($"Публичное поле: {FieldOne}"); } private void Write(string text) { FieldTwo = text; } }
What do we see here?
- We have a
public
field (Field) to which we have access from all the methods of our class, as well as from other classes that initialize this class. - We have a
private
field (Field) to which we have access from all methods of our class, but we will not be able to set / get the value of this field from the outside. - We also have two methods, one
public
and the otherprivate
, everything is the same with them; there is noprivate
access from outside, but there is access inside the class;public
- we have access from all possible points.public
Read method in this case calls theprivate
Write method and outputs the value of two Field to the console.private
Write method simply sets the value of the text passed to ourprivate
field.
Now let's make a call to all of this:
MainClass mainClass= new MainClass(); mainClass.FieldOne = "Привет землянин!"; mainClass.Read();
It can be noted that after initializing our class ( new...
), we can easily work with all public fields and methods that are specified in the root of this class.
In the meantime, the console will display:
Приватное поле: Hello! Публичное поле: Привет землянин!
Now suppose we have a method in a certain class:
public void MyMethod() { int MyField; }
Question: Can we access MyField
from other classes, methods, etc.?
The answer is no. This is a local field with which only this method can work!
Summarize:
- If we need to use a field outside the method, then it should be placed in the body of the class with the indication of the access modifier.
- If we go so that other classes could use our field (beyond the limit of the current class), then it is worth indicating the corresponding access modifier (for example,
public
).
Now let's talk about transferring values between classes:
Create another class, it should be able to work with everything that our MainClass
:
public class MySecondClass { private MainClass MainClass; public MySecondClass(MainClass MainClass) { MainClass = MainClass; } public void Read() { MainClass.Read(); } }
What we have?
private MainClass MainClass;
- We set the object we need, to which everything that is inside the current class has access. But it is initially null, it must be initialized or set a value.public MySecondClass(MainClass MainClass)
is the so-called "constructor", everything inside it will be executed during initialization (that is, withnew MySecondClass();
). By the constructor, we can accept what is necessary for the work of this class (in this case it is a different class).MainClass = MainClass;
- When writingnew MainClass()
, we create an object, but what will happen if we prescribe it several times? Yes, everything is simple, every time the object is initialized, all its internal values will be initial (that is, those that we specify, or Null). In some cases, we need several identical objects, and in some people by mistake they create a duplicate (of the same form, for example), when you can use the already created and send a link to it. Here, in this case, we pass the link with the parameterMainClass
, which is passed to our privateMainClass
in the constructor.public void Read()
- everything is just becoming, a simple public method that calls a public method from another class.
Let's rewrite a bit of the challenge of all this:
MainClass mainClass = new MainClass(); MySecondClass mySecondClass = new MySecondClass(mainClass); mainClass.FieldOne = "Привет землянин!"; mySecondClass.Read();
We first initialize our main class (MainClass), then initialize the second class (MySecondClass) into which we pass the reference to the main class, set the value in the main class public field and call our Read method from the second (and not the first) as before. The result will be the same text.
Well, everything seems to be, I hope I helped you with this "educational program", explained about the availability of methods or fields, as well as with the correct transfer of values between classes. Good luck learning C #!
- @ y47999 and? Firstly, it’s not very convenient to read the code like that, and secondly, well, everything seems to be normal, what are the difficulties? - EvgeniyZ
- Yes, I did it by the example, but my form does not load @ EvgeniyZ - y47999
- @ y47999 Well, it can’t just not boot, I suppose there’s an error or something else? - EvgeniyZ
- everything seems to be fine @EvgeniyZ - y47999
- @ y47999 Great! Glad to understand. Do not forget to tick the answer if it helped you. - EvgeniyZ
public int width_of_nonogram;
in another window send a link to the EnterWidthAndHeight window and already work with this link. - EvgeniyZ