5

I have the following code:

if (Current == false)
{
    foreach (var visual in visuals)
        visual.IsSelected = value;
}

Visual visual = visuals[currentIndex];

And when I compile I have this error:

A local variable named 'visual' cannot be declared in this scope because it would give a different meaning to 'visual', which is already used in a 'child' scope to denote something else

Also, if I don't declare the visual variable, that is I replace:

Visual visual = visuals[currentIndex];

with:

visual = visuals[currentIndex];

the error is the following:

The name 'visual' does not exist in the current context

Why this behavior?

Nick
  • 10,309
  • 21
  • 97
  • 201
  • 1
    `which is already used in a` **child** `scope to denote something else` Since in your `foreach` loop has a variable called `visual`, that's why you get this error. – Soner Gönül Apr 05 '14 at 11:08
  • @SonerGönül the scope of the "first" visual is not limited inside the if statement? – Nick Apr 05 '14 at 11:17
  • 2
    Check out Eric Lippert's answer.. http://stackoverflow.com/a/2050864/447156 – Soner Gönül Apr 05 '14 at 11:27
  • It is by design of C#. It reduces ambiguity. Similar [question][1] [1]: http://stackoverflow.com/questions/2059210/using-a-variable-name-used-in-a-child-scope – Balu Apr 05 '14 at 11:27

4 Answers4

1

Why this behavior?

in your first case you have already declared the variable with name visual in your foreach loop.

in your second case you can not use the keyword visual because it does not exist. it is only available within your foreach loop.

Try This:

Visual visual1 = visuals[currentIndex];
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

In the first case there is ambiguity between the variables declared outside and inside.(global and local).

The compiler is confused as to what visual you are referring to. The outer one or inner one?

And in the second case, the compiler does not know what is visual.

Read more about it here;

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1
   if (Current == false)
    {
        foreach (var visual in visuals)
            visual.IsSelected = value;
    }

   // Parent and child scope have same variable name so it creates ambiguity.
    Visual visual = visuals[currentIndex];

and

if (Current == false)
    {
        foreach (var visual in visuals)
            visual.IsSelected = value;
    }

   // The variable visual in not defined outside the scope of if statement
    visual = visuals[currentIndex];
Octane
  • 1,200
  • 11
  • 11
1

As Soner Gönül points out, the first construct:

if (Current == false)
{
    foreach (var visual in visuals)
        visual.IsSelected = value;
}

Visual visual = visuals[currentIndex];

is illegal by definition of the language. See the link that Soner provided:

https://stackoverflow.com/a/2050864/447156

Making this illegal reduces the chance of confusion when humans read the code. The compiler could treat this as legal, but the designers of the language felt (and I agree) that this was an opportunity to make C# easier to understand.

Community
  • 1
  • 1
KevinS
  • 242
  • 1
  • 7