1
\$\begingroup\$

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 ?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Servy Mar 5 '13 at 18:05
  • \$\begingroup\$ @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 \$\endgroup\$ – Steve G Mar 5 '13 at 18:06
  • \$\begingroup\$ 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? \$\endgroup\$ – Leonid Mar 7 '13 at 21:53
  • \$\begingroup\$ i'm making buttons edit change picture change text etc \$\endgroup\$ – adi sba Mar 11 '13 at 13:07
4
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

 }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.