met.lord
met.lord

Reputation: 618

A simple string array Iteration in C# .NET doesn't work

This is a simple code that should return true or false after comparing each element in a String array with a Session Variable. The thing is that even when the string array named 'plans' gets the right attributes, inside the foreach it keeps iterating only over the first element, so if the Session Variable matches other element different than the first one in the array it never returns true... You could say the problem is right there in the foreach cicle, but I cant see it... I've done this like a hundred times and I can't understand what am I doing wrong... Thank you

protected bool ValidatePlans()
{
    bool authorized = false;

    if (RequiredPlans.Length > 0)
    {
        string[] plans = RequiredPlans.Split(',');
        foreach (string plan in plans)
        {
            if (MySessionInfo.Plan == plan)
                authorized = true;
        }
    }
    return authorized;
}

Upvotes: 1

Views: 1066

Answers (8)

Ryan Christensen
Ryan Christensen

Reputation: 7933

Make sure RequiredPlans truly has unique string items, i.e. make sure it isn't returning Blue, Blue, Blue.

Second like others mentioned make sure to .Trim() the compare and why not throw in a .ToLower() to make sure there isn't case sensitive issues.

If all that is in it should clearly work. If not create a temporary RequiredPlans string to be sure it works as an individual test.

protected bool ValidatePlans()
{   
    if (RequiredPlans.Length > 0)
    {
        string[] plans = RequiredPlans.Split(',');
        foreach (string plan in plans)
        {
            if (MySessionInfo.Plan.Trim().ToLower() == plan.Trim().ToLower())
                return true;
        }
    }

    return false;
}

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128307

I'm not 100% sure I understand your problem, but I wonder if you're using a string like:

"Blue, Green"

...and expecting this method to return true when MySessionInfo.Plan is "Green" (no space). If this is the problem then it's just a simple matter of there being an extra space in your Split result. This could be fixed by changing this:

if (MySessionInfo.Plan == plan)

...to this:

if (MySessionInfo.Plan == plan.Trim())

If that's not it, then maybe I've misunderstood your problem.

Edit: From your comment to HABJAN's answer, I have to ask: is the code you posted exactly the same as the actual code you're using? If so I have to scratch my head, as it is very unclear how the modification he suggested (though it's a good one, as it makes the method more efficient) could have fixed your problem.

Upvotes: 0

heisenberg
heisenberg

Reputation: 9759

Sure, RequiredPlans is a String like ="Blue, Green"

If this is what your input is looking like, then I think the problem isn't that you are iterating over the first value multiple times, but that you aren't trimming the values. So everything after the first has a leading space and the match fails.

Try

if (MySessionInfo.Plan == plan.Trim())

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

You might want to consider using Array.IndexOf() instead of doing all the work yourself:

protected bool ValidatePlans()
{
    return (RequiredPlans.Length > 0
        && Array.IndexOf(RequiredPlans.Split(','), MySessionInfo.Plan) >= 0);
}

Upvotes: 1

Lee
Lee

Reputation: 144136

Your code looks fine to me, but you could replace the whole thing with:

return RequiredPlans.Length > 0 && RequiredPlans.Split(',').Any(s => MySessionInfo.Plan == s);

Upvotes: 1

The Fat Oracle
The Fat Oracle

Reputation: 646

A few possibilities:

A. Assuming MySessionInfo.Plan is not strongly typed as a string, try

if(MySessionInfo.Plan.Equals(plan)) { authorized = true; break; }

B. After doing the .split, during compare make sure there are no trailing spaces. You could do a MySessionInfo.Plan.Equals(plan.Trim())

C. Not sure what "foreach it keeps iterating only over the first element," means. Are you sure the loop doesn't go through the other elements in the array?

Upvotes: 0

HABJAN
HABJAN

Reputation: 9338

protected bool ValidatePlans()
{   
    if (RequiredPlans.Length > 0)
    {
        string[] plans = RequiredPlans.Split(',');
        foreach (string plan in plans)
        {
            if (MySessionInfo.Plan == plan)
                return true;
        }
    }

    return false;
}

Upvotes: 0

stephen776
stephen776

Reputation: 9234

I believe you will need to explicity set the length of the the string array "plans" before adding values via the string.split...

EDIT: No I was wrong...your method of populating the array should work fine. Ignore my answer

Upvotes: 0

Related Questions