# Get Set vs passing class at init?

I have the main form with a list inside and i need to open another form and edit the list in the main. Generally i'm doing something like ...

    public class Form1 : Form
{
List<objectType> obj = new List<objectType>();
private void button_EDIT_BUTTONS_Click(object sender, EventArgs e)
{
EDIT_BUTTONS edit_b = new EDIT_BUTTONS(this);
edit_b.Show();
}
}
public partial class EDIT_BUTTONS : Form
{
Form1 main;
public EDIT_BUTTONS(Form1 mainP)
{
InitializeComponent();
main = mainP;
}
button = main.obj.Find(x => x.ID == object.ID);
............................................................
//modify button

}


My question is to pass the main form like this only to expose the list object or i should Create setters and getters in main form ?

• First you should really work on your naming conventions. Class names should start with an upper case letter, not be in full caps, and use camel casing to denote words, rather than underscores. It makes the code much easier to read for everyone else who's used to those conventions. – Servy Mar 5 '13 at 18:05
• @Servy, I aswered this question a few weeks ago and you busted me for saying "pass the whole form". Haha got it right this time and was thinking of you when I wrote it. Funny that you should comment at the same time – Steve G Mar 5 '13 at 18:06
• Could you provide specific information about what you are doing with the buttons, what these buttons are for and what the forms do in general? – Leonid Mar 7 '13 at 21:53
• i'm making buttons edit change picture change text etc – adi sba Mar 11 '13 at 13:07

You should only pass the list to the second form. If you pass the entire first form you are exposing somethings that wouldn't be best, and coupling the two forms tightly.

It would be better if an object was used in a encapsulated method of Form1.

Create a method inside Form1, something like GetButton(string Id) and return the button object only, no need to expose the whole list.

 public class Form1 : Form
{
List<objectType> obj = new List<objectType>();
private void button_EDIT_BUTTONS_Click(object sender, EventArgs e)
{
EDIT_BUTTONS edit_b = new EDIT_BUTTONS(this);
edit_b.Show();
}
public Button GetButton(string id)
{
return  obj.FirstOrDefault(x => x.ID == Id);
}

}
public partial class EDIT_BUTTONS : Form
{
Form1 main;
public EDIT_BUTTONS(Form1 mainP)
{
InitializeComponent();
main = mainP;
}
button = main.GetButton(object.ID);

//modify button

}