-1

I´m trying to create new Controls (TextBox, ComboBox and CheckBox) to a Control.ControlCollection, but it doesn´t work. Normally my WinForm would pass its controls to that method, but now I´m trying to write a unit test for it.

Here´s the code:

        TestClass target = new TestClass(); 
        Control.ControlCollection controls = null;
        CheckBox checkBox = new CheckBox();
        checkBox.Name = "SomeCheckBox";
        checkBox.Checked = true;
        ComboBox comboBox = new ComboBox();
        comboBox.Name = "SomeComboBox";
        checkBox.Text = "Some text in CB";
        TextBox count = new TextBox();
        count.Name = "CountTextBox";
        count.Text = "20";
        TextBox date = new TextBox();
        date.Name = "DateNow";
        date.Text = System.DateTime.Now.ToString("dd.MM.yyyy");
        controls.AddRange(new Control[] {checkBox, comboBox, count, date });
        string actual;
        actual = target.saveEverything(controls);

Test fails in the AddRange-Row. What mistake did I make?

Smurf
  • 83
  • 5
  • 1
    You never initialize `controls`. – gunr2171 Jun 19 '14 at 14:16
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – gunr2171 Jun 19 '14 at 14:16
  • 1
    Make that `Control.ControlCollection controls = new Control.ControlCollection ();` – TaW Jun 19 '14 at 14:19
  • @TaW You forgot that new Control.ControlCollection() needs a Control to pass on, but now it works. – Smurf Jun 19 '14 at 14:27
  • @Smurf, now that you have solved your own problem, (and if you want) post your own answer using the button below. – gunr2171 Jun 19 '14 at 14:28
  • @gunr2171 "Users with less than 10 reputation can't answer their own question for 8 hours after asking." :( – Smurf Jun 19 '14 at 14:36
  • @Smurf, don't worry about it. Just come back in 8 hours and post the answer (this gives other people time to voice their input). In the mean time, keep programming or [read a good book](http://www.manning.com/skeet3/?a_aid=jonskeet&a_bid=66d590c3). – gunr2171 Jun 19 '14 at 14:39

1 Answers1

1

Ok, I´m stupid. I forgot to initilize controls.

Control con = new Control();
Control.ControlCollection controls = new Control.ControlCollection(con);
Smurf
  • 83
  • 5