Julian Johnson
Julian Johnson

Reputation: 42

Creating a string array to call later in C#

Creating a remote installer application in C#. First, I am doing a ping check to make sure the computer is alive before i run the install on it. I am going to implement service checks later.

I need to create a string array that will be based on the ping status being a success (goodPing) and a failure (badPing), so I can use it in another foreach loop where the installs will run.

foreach (string singleComputer in computerArray)
        {
            // Ping all computers
            Ping ping = new Ping();
            PingReply pingresult = ping.Send(singleComputer);

            if (pingresult.Status.ToString() == "Success")
            {
               string[] goodPing = new string[] {"singleComputer"};
            }
            else if (pingresult.Status.ToString() != "Success")
            {
               string[] badPing = new string[] {"singleComputer"};
            }
        }

Basically, I am making sure the computer is pingable, separating the "good" and "bad" computers. computerArray is a string array created from a file.

This is what I want it to look like after I create the arrays:

foreach (string computer in goodPing)
{
     //run installation code here.
}

Obviously, I cannot call goodPing because it is created in an if statement. Please enlighten me on what I am doing wrong.

Upvotes: 0

Views: 113

Answers (2)

Vahid
Vahid

Reputation: 1949

You need to use generic lists instead of arrays and you need to declare them before the for loop so they will be accessible after it.

List<string> goodPings = new List<string>();
List<string> badPings = new List<string>();
foreach (string singleComputer in computerArray)
        {
            // Ping all computers
            Ping ping = new Ping();
            PingReply pingresult = ping.Send(singleComputer);

            if (pingresult.Status.ToString() == "Success")
            {
               goodPings.Add(singleComputer); // and don't forget to use the variable singleComputer here, not the literal string "singleComputer".
            }
            else if (pingresult.Status.ToString() != "Success")
            {
               badPings.Add(singleComputer);
            }
        }

Upvotes: 0

dotnetom
dotnetom

Reputation: 24901

You need to create you array once, before running the loop. The issue with arrays is that there is no easy way to change the size of the array, and you don't know in advance how many records in each array you will have. So I suggest using List<T> instead of arrays:

List<string> goodPing = new List<string>();
List<string> badPing = new List<string>();
Ping ping = new Ping();

foreach (string singleComputer in computerArray)
{
    // Ping all computers
    PingReply pingresult = ping.Send(singleComputer);

    if (pingresult.Status.ToString() == "Success")
    {
         goodPing.Add(singleComputer);
    }
    else if (pingresult.Status.ToString() != "Success")
    {
         badPing.Add(singleComputer);
    }
}

And now you can use the list of good pings to do what you need to do:

foreach (string computer in goodPing)
{
     //run installation code here.
}

Upvotes: 2

Related Questions