Re: simple example
- From: "Tony Johansson" <johansson.andersson@xxxxxxxxx>
- Date: Wed, 16 Sep 2009 15:02:46 GMT
Hello!
I still don't fully understand why this cause compile error.
I can't understand why not the first WriteLine use the class variable x and
the second will use the local variable x.
I think is very clear and can't understand why the compiler would complain.
class Foo
{
static int x = 123;
static void Main(string[] args)
{
Console.WriteLine("x är {0}", x);
int x = 1;
Console.WriteLine("x är {0}", x);
}
}
I can understand that this compile fine because you have explicitly stated
that the first WriteLine should use
the class variable x. So when you declared the local variable x this will be
used in the
second writeLine. So this is pretty easy to understand.
class Foo
{
static int x = 123;
static void Main(string[] args)
{
Console.WriteLine("x är {0}", Foo.x);
int x = 1;
Console.WriteLine("x är {0}", x);
}
}
This example is also easy to understand
static void Main(string[] args)
{
int x = 1;
{ int x = 1; }
}
//Tony
"Patrice" <http://scribe-en.blogspot.com/> skrev i meddelandet
news:OOFB2SsNKHA.2036@xxxxxxxxxxxxxxxxxxxxxxx
The OP tells actually that the code compiles fine but was expecting this
compile time error...
This is before this message is for *local variables* (which is what you
described). It doesn't apply to the OP case (i.e. he doesn't try to use
two local variable in the same scope but a local variable and a static
member that has the same name in a parent scope which is allowed).
This is further complicated as it seams the OP thought the location of the
local variable declaration inside the function is meaningfull (i.e. before
the declaration, x would refer to the static member, and after x would
refer to the local variable) when it is not...
I tried to elaborate in another response I just posted...
--
Patrice
"Alberto Poblacion" <earthling-quitaestoparacontestar@xxxxxxxxxxxxx> a
écrit dans le message de groupe de discussion :
e89x$xrNKHA.5108@xxxxxxxxxxxxxxxxxxxxxxx
"Tony Johansson" <johansson.andersson@xxxxxxxxx> wrote in message
news:h62sm.11612$U5.154437@xxxxxxxxxxxxxxxxxx
I mean that the compiler wouldn't have the slighest idea which x I refer
to when simply writing x.
In traditional versions of the C languaje and its derivatives, the
compiler always uses the version of x that is in the most internal scope
to the statement that uses it (for instance, it uses a local x instead of
an x declared as a member variable of the class).
As you have already noted, this can be confusing and can cause errors
when the variable that is being used is not the one that you had in mind
when you wrote the code. For this reason, the designers of C# chose to
forbid this situation, and therefore the compiler does not let you define
inside a scope a variable that has the same name as another variable in
an enclosing scope. This is why you get the error "A local variable named
'x' cannot be declared in this scope because it would give a different
meaning ..."
.
- Follow-Ups:
- Re: simple example
- From: Patrice
- Re: simple example
- References:
- simple example
- From: Tony Johansson
- Re: simple example
- From: Alberto Poblacion
- Re: simple example
- From: Patrice
- simple example
- Prev by Date: Need moveable toolstrip example code - New to Windows & C#
- Next by Date: Re: Search through a (large) binary file.
- Previous by thread: Re: simple example
- Next by thread: Re: simple example
- Index(es):
Relevant Pages
|