-1

On start, I want my program to check services status from a list in an array and then add them to two new textboxs:

    public string[] wantedServices = { "MSSQL$SQL", "SQLBrowser" };
    public TextBox[] txtserviceStatus = new TextBox[2];

    //Gets Service Status On Load
    public void StartServiceStatus()
    {
        int i = 0;
        ServiceController[] services = ServiceController.GetServices()
                      .Where(svc => wantedServices.Contains(svc.ServiceName))
                      .ToArray();
        foreach (ServiceController sc in services)
        {
            txtserviceStatus[i].Text = sc.Status.ToString();
            this.Controls.Add(txtserviceStatus[i]);
            txtserviceStatus[i].Left = 0;
            txtserviceStatus[i].Top = (i + 1) * 20;
            i++;
        }
    }

When I step through the code, it only does the first line and doesn't break the rest within the foreach loop

Any advice would be greatly appreciated as I am still new to this and want to achieve 'good code'

Many Thanks

SCramphorn
  • 447
  • 4
  • 23
  • How exactly is this failing? What does "it only does the first line" mean? – David Jun 27 '16 at 10:25
  • Hi @David, when I step through the code it it will only run `txtserviceStatus[i].Text = sc.Status.ToString();` after using a try & catch it now shows me 'obj ref not set to an instance' – SCramphorn Jun 27 '16 at 10:27
  • 1
    You'll have to add `txtserviceStatus[i] = new TextBox();` at the beginning of the loop, because you're just creating an array of textboxes without initializing an instance of the class for each one. – D. Petrov Jun 27 '16 at 10:32
  • @D.Petrov fantastic, thank you. How come it didn't recognize it as it's a public textbox with: `public TextBox[] txtserviceStatus = new TextBox[2];` – SCramphorn Jun 27 '16 at 10:36
  • Problem is: you're creating just an array of objects of the class `TextBox` but you're actually not creating any instance of the class, so basically there's no textbox created yet, just an array to hold such. – D. Petrov Jun 27 '16 at 10:37
  • @D.Petrov thank you, that makes sense and will help me in the future. One last question if I may, when I don't comment out the `i++` (to select the next array) I get the same error of Obj ref not set... What am I doing wrong here? – SCramphorn Jun 27 '16 at 10:41
  • @SCramphorn couldn't understand your issue. You mean, you've already created the instance of the first object in your array and want to just replace the data with the next service controller, so you're using the same already created instance and it gives you the error? You could show code to be more precise. – D. Petrov Jun 27 '16 at 10:43

2 Answers2

1

You're basically just creating an array of objects of the class TextBox, but afterwards you're using the objects without connecting any instance of the class to them. So, all you'll have to do is to just add txtserviceStatus[i] = new TextBox(); at the beginning of your loop, so that there would be an instance of the textbox to work with.

D. Petrov
  • 1,147
  • 15
  • 27
  • 1
    I put it at the start of the class and not loop. This has worked brilliantly, thank you for your help and haste! – SCramphorn Jun 27 '16 at 10:48
1

When you create the array txtserviceStatus = new TextBox[2] the values in that array are all null by default. You need to create the items in the array yourself:

public string[] wantedServices = { "MSSQL$SQL", "SQLBrowser" };
public TextBox[] txtserviceStatus;

//Gets Service Status On Load
public void StartServiceStatus()
{
    txtserviceStatus = ServiceController.GetServices()
                  .Where(svc => wantedServices.Contains(svc.ServiceName))
                  .Select(sc => sc.Status.ToString())
                  .Select(CreateTextBox)
                  .ToArray();
    txtserviceStatus.ForEach(this.Controls.Add);
}

private TextBox CreateTextBox(string text, int i) {
    TextBox textBox = new TextBox();
    textBox.text = text;
    textBox.Left = 0;
    textBox.Top = (i + 1) * 20;
    return textBox;
}
gfv
  • 659
  • 5
  • 11