Instance name Default of Form

A

AMP

Hello,
I am trying to find out the name of the default form and if Im doing
this right
Form2 ( is startup form, not form 1 ):

public class Form2 : System.Windows.Forms.Form
{
private Form1 myForm1 = null;
..........................................
public Form2()
{
InitializeComponent();

}
...............................................................
public void createform()
{
this.myForm1 = new Form1(this);
this.myForm1.Show();

}
private void button1_Click_1(object sender,
System.EventArgs e)
{
this.createform();
}
static void Main()
{
Application.Run(new Form2());
}





Form1:
public class Form1 : System.Windows.Forms.Form
{
private Form2 myForm2 = null;
.............................................................................


public Form1(Form2 frm2)
{ myForm2=frm2;
InitializeComponent();
}

private void button2_Click(object sender,
System.EventArgs e)
{
myForm2.label1.Text = "diff";
}
So you know what I am doing(and it works):
a button on form2 creates form1 and a button on form1 changes some text
on form2

My questions are
The form I send to form1 I send as "this" and it works.Does it have
another instance name?
I have a reference in both forms to the other form, do I need that.It
doesnt seam to work withou it.And most of all....This this the most
professional method of doing this?
Thanks
Mike
 
D

dkode

From Form2, AFAIK you can only pass it as "this".

This approach is not a true OOP approach, in reality you would have
relationships setup between the two classes, that allows for loose
coupling.

It will work for what you need it to do, but is it the "correct" way?
Thats a matter of opionon.

At the very least you should have properties setup in each for a
unified way of accessing the references to the other form:

private Form2 _myForm2;
private Form2 MyForm2 {
get { return _myForm2; }
}

Form1(Form2 form2) {
_myForm2 = form2;
}

ALWAYS try to use properties to access references/values. Later down
the line, if you need to add some additional stuff to the _myForm2
reference, you can do so in the get accessor of MyForm2, this is called
encapsulation or "data-hiding". Always at the very least try to perform
some level of encapsulation, eventually it will make your life much
easier
 
A

AMP

Thanks dkode,
And the other questions? The default instant name?Do I NEED to
reference both forms in each other within the class def.?
Thanks
Mike
 
D

dkode

I dont know about a default instance name, I always use "this"

As far as the refernece to each other on both forms, Do you need to
access something on Form2 from Form1, do you need to acces something on
Form1 from Form2?

It depends on how they will relate to each other, if you dont need to
access anything on form1 from form2, then there is no need for a
reference to form1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top