Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
A local variable named 'Status' cannot be declared in this scope because it would give a different meaning to 'Status' , which is already used in a 'parent or current' scope to denote something else
Posted
Updated 27-Jan-11 19:40pm
v2

C#
bool status = true;
if (status == true)
{
    for (int status = 0; status < 1; status++)
    {
        //Do something
    }
}


in this case status is already declared in the if block, so it can not be declared in the child block (i.e. For block).
Every variable has a scope associated with it, and it is limited to the block in which it is declared.

e.g.

static main()
{
    {
       int i = 0;
    }

    //i is not accessible here because it is declared in the different block.
}


With this, no two variables with the same name can be declared in the scope. If you do so, you get an error saying : "A local variable named 'xyz' cannot be declared in this scope because it would give a different meaning to 'xyz' , which is already used in a 'parent or current' scope to denote something else"
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jan-11 4:14am    
That is correct example - my 5. However, OP did not bother to show any code -- why I should? (See my answer.)
--SA
Pravin Patil, Mumbai 28-Jan-11 4:58am    
Thanks SA. OP should have provided some code. It helps to answer.
Sergey Alexandrovich Kryukov 28-Jan-11 13:11pm    
I think I provided the ultimate recipe of how to fix the problem, please see my answer.
Agree?
--SA
Pravin Patil, Mumbai 31-Jan-11 7:45am    
Yeah...
Rahul Jain, Serious Coder 31-Jan-11 13:22pm    
I Liked your answer..
Just rename it. Mind visibility and scope. Not sure what is that? — just rename it.

—SA
 
Share this answer
 
v3
Comments
honey4bee 28-Jan-11 1:41am    
k that is Status
Sergey Alexandrovich Kryukov 28-Jan-11 1:46am    
Wow! Not in your question, in your code!

You have two variables of the same name, one in some inner scope, another in some outer scope. Located them. Rename the variable in inner scope.
Sergey Alexandrovich Kryukov 28-Jan-11 1:50am    
The reason of the compiler message is it cannot resolve which variable is addressed in some inner scope: the one declared in some inner scope or the one declared in some outer scope -- there is some ambiguity. Rename one of them, then decide which one to use in your inner scope.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900