Re: simple example

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



This is unrelated with static int x=123.

This is because you declared a local variable x at some point in your code.
But technically speaking a local variable is tied with the current block of
code (or more precisely function) as if all declarations were at the top of
your function.

So using x even before the declaration means that you are trying to use
"x_the_local_variable" (and not only below the point where it is declared as
you perhaps thought). The compiler then tells :
"Cannot use local variable 'x' before it is declared."

With this :
static void Main(string[] args)
{
int x = 1;
{ int x = 1; }
}

You'll get :
A local variable named 'x' cannot be declared in this scope because it would
give a different meaning to 'x', which is already used in a 'parent or
current' scope to denote something else

but is once again unrelated to static int x=123. This is because a local
variable is tied to the current function so even though it is in a inner
block of code, you still have two x local variables named the same way in
this function which is not possible...

An important part from the error message is *local variable* i.e. it is
forbidden because this is another local variable. You are still free to use
the same name for something else (such as a static member) in a parent
scope... I believe this is what could have caused the initial confusion.

So to summarize :
- a local variable is scoped to the current function (even if declared in an
inner block of code inside the function such as a loop) and should be
uniquely named inside this function
- it should be declared before being used (and declaring a variable
somewhere else than at the top of a function is just a conveniance, it
doesn't mean that what is in the current scope changes at this exact point,
technically speaking all local variable declaration are handled as if they
were at the top of the function)
- you are allowed to declare a local variable (so by definition scoped to
the function) and other members tied to other classes with the same name as
you'll always be able to access this last one by using a fully qualified
name...


--
Patrice




.



Relevant Pages

  • Re: question about assigning null to a reference object
    ... for local variables, then they are not the same. ... The following code will also fail to compile, ... it is best to not initialize it at all. ... them instinctively initialize all variables at their declaration. ...
    (comp.lang.java.help)
  • Re: Static variables in ASP .NET/VB .NET
    ... Specifies that one or more declared local variables are to remain in existence and retain their latest values after termination of the procedure in which they are declared. ... This means the declaration context for a Static variable must be a procedure or a block within a procedure, and it cannot be a source file, namespace, class, structure, or module. ... You cannot use Static inside a structure procedure. ... You call a Shared procedure using the class name, not a variable pointing to an instance of the class. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: simple example
    ... I still don't fully understand why this cause compile error. ... This is before this message is for *local variables* (which is what you ... member that has the same name in a parent scope which is allowed). ... local variable declaration inside the function is meaningfull (i.e. before ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Reusing the name of local variable
    ... it's not accessible before its declaration. ... Doesn't compiler see that these are two different variables? ... It considers them to be two different local variables with the same ... with one of them in the scope of the other. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: already used in a child scope to denote something else
    ... The scope of a local variable declared in a local-variable-declaration ... is the block in which the declaration occurs. ... The scoping rules for local variables are designed to guarantee ... that the meaning of a name used in an expression context is always the ...
    (microsoft.public.dotnet.languages.csharp)