Carl D.
Carl D.

Reputation: 31

For loop to run through values

I am trying to run a for loop to run through values, but I am not sure of how to do this correctly.

        string[] GoogleID = { "ga:1381000", "ga:1860066"};

        // Loop with the foreach keyword.

        foreach (string value in GoogleID)
            {


            if (GoogleID.ToString() == "ga:1381000")
            {
                WebName = "Yes";
            }
            else
            {
                WebName = "No";
            }


            }

What am I doing wrong? How do I make it check both values?

It says my string for GoogleID.ToString = String[]

Upvotes: 3

Views: 100

Answers (7)

Mark G
Mark G

Reputation: 597

Each time through the loop it was checking against first element in your group (GoogleID or GoogleID[0]). Thus always true and always printing "yes" It should be checking against "value". See the minor changes below. I changed "value" to X to highlight it.

string WebName;

    string[] GoogleID = { "ga:1381000", "ga:1860066"};

    // Loop with the foreach keyword.

    foreach (var X in GoogleID)
        {
            if (X == "ga:1381000")
            {
                WebName = "Yes";
            }

            else
            {
                WebName = "No";
            }
        }

Fully functioning version here: https://dotnetfiddle.net/lihDeY

Upvotes: 0

Benjamin RD
Benjamin RD

Reputation: 12034

var WebName = GoogleID.Contains("ga:1381000") ? "Yes" : "No"

Upvotes: 0

Pratik
Pratik

Reputation: 17

You can use the for loop as shown below:-

 string[] GoogleID = { "ga:1381000", "ga:1860066" };
          //use for loop
          for (int i = 0; i < GoogleID.Length; i++)
         {
           if (GoogleID[i].ToString() == "ga:1381000")  //use index here 
            {
                WebName = "Yes";
            }
            else
            {
                WebName = "No";
            }
        }

Upvotes: 0

Alexander Bell
Alexander Bell

Reputation: 7918

You should correct your code as shown in the following sample code snippet:

    string[] GoogleID = { "ga:1381000", "ga:1860066" };
    string WebName;

    // Loop with the foreach keyword.
    foreach (string _val in GoogleID)
    {
        WebName = (_val == "ga:1381000") ? "Yes" : "No";
    }

For better performance you may use the following code snippet:

    string[] GoogleID = { "ga:1381000", "ga:1860066" };
    string WebName;

    // Loop with the for keyword.
    for (int i = 0; i < GoogleID.Length; i++ )
    {
        WebName = (GoogleID[i] == "ga:1381000")? "Yes":"No";
    }

Hope this may help.

Upvotes: 1

CrnaStena
CrnaStena

Reputation: 3167

Yet another LINQ suggestion.

WebName = GoogleID.Contains("ga:1381000") ? "Yes" : "No"

Upvotes: 1

w.b
w.b

Reputation: 11228

You can use LINQ:

WebName = GoogleId.Any(s => s == "ga:1381000") ? "Yes" : "No";

Upvotes: 6

Vivek N
Vivek N

Reputation: 991

You would want to do it like this.

string[] GoogleID = { "ga:1381000", "ga:1860066"};

        // Loop with the foreach keyword.

        foreach (string value in GoogleID)
            {


            if (value  == "ga:1381000")
            {
                WebName = "Yes";
            }
            else
            {
                WebName = "No";
            }


            }

Upvotes: 7

Related Questions