BruceyBandit
BruceyBandit

Reputation: 4324

local or parameter error as it is within scope

I am struggling to understand and fix this error I have where I am trying to grab numbers from a text.

VIrtually I have two pieces of text from alternativeAirportPrice.Text. One says:

+156 on same day

So I want to output only 156 from this text the other says:

-156 on same day

So I want to output only - 156 from this text

I implement the foreach method to use a char.IsNumber(), however I am receiving an error for c within Select(c => c.toString())

The error is:

a local or parameter name 'c' cannot be declared in this scope because that name is TestSuite used in an enclosing local scope to define a local or parameter.

What do I need to do to fix this and be able to spit out just the numbers from the text?

public string GetAlternativeAirportPrice(By airportPriceLocator)
{
    var alternativeAirportPrices = _driver.FindElements(airportPriceLocator);

    foreach (var alternativeAirportPrice in alternativeAirportPrices)
    {
        if (alternativeAirportPrice.Displayed)
            foreach (char c in alternativeAirportPrice.Text)
            {
                if (char.IsNumber(c))
                {
                    alternativeAirportPrice.Text.ToCharArray().Select(c => c.ToString()).ToArray();
                }
            }
        return alternativeAirportPrice.Text;
    }
    return null;
}

Upvotes: 1

Views: 82

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39946

This is because you have already declared c in your code in the following line:

foreach (char c in alternativeAirportPrice.Text)

So you can't use it in your Select again. Try use something else instead, for example x:

.Select(x => x.ToString())

Or try renaming c to something else inside the foreach, for example item:

foreach (char item in alternativeAirportPrice.Text)
{
    if (char.IsNumber(item))

Upvotes: 3

Related Questions