which code block to write and where to reach and assign?
hi to all sorry for the basic question but i will be happy if you can explain some Qs below. thanks.
namespace forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// in thic code block what kind of things can i write or im allowed to write? Q1
}
Form2 frm2 = new Form2(); // why should i write this line (since i already added form2 to my project as seen in the picture) ![enter image description here][1]to see frm2.Show(); in button1_Click part? Q2
//what happens in the background (from the compiler point of view) when i do Form2 frm2 = new Form2();? Q3
frm2.Show(); // why cant i reach frm2 in here? i just declared above. Q4
//just like that
int number1; // i declare number variable in here
number1= 5; // and why cant i assign number in here?
// what kind of things can i write or allowed to write in this block ? Q5
// i sometimes confuse where i need to start writing the code or where i need to write or in which block ?
public int number2;
// ok now lets say i put a button on the form and when i double click it generated the code down below
//and now lets look in that code block
private void button1_Click(object sender, EventArgs e)
{
// ok now we are in this block and now it see when i write
frm2.Show();
//or
//it see when i write
number1 = 5;
//ok now lets look at number1 and number2 what changes when i write public int and just int without public? Q6
}
}
}

First your code shouldn’t compile on line:
and
These lines should be part of a method.
Now to your questions.
Q1. That’s the constructor block of
Form1class. You may do initialization and other stuff you want to be executed on first execution of theForm1.Q2. Your line
is creating an instance for
Form2, although you add the file to the project, to useForm2you have to create an instance of it first.Q3. The above line is creating an instance of
Form2, calling the constructor forForm2and assigning the the instance ofForm2to its referencefrm2Q4. You can’t do
frm2.Show();, Only field definition and initialization can be done at class level. This line should be part of some method.Q5. Same answer as Q4
Edit:
Q6. If you specify
publicwith int, that field will be accessible outside the class, and if you don’t specify anything then it isinternalby default.