Really weird 5 line scope-related problem

  • Thread starter Thread starter Afzal Mazhar
  • Start date Start date
A

Afzal Mazhar

What is wrong with this?

class Example1
{
public static long myClassVar;

public static void Main()
{
{
long myClassVar = 5;
}
myClassVar = 36;
}
}

It will not compile.....why? I do not need a "fix" but rather am confused
why this will not compile.
It gives an error at myClassVar=36 that is conflicts with declaration
Example1.myClassVar

Thanks

Afzal
 
Afzal Mazhar said:
What is wrong with this?

class Example1
{
public static long myClassVar;

public static void Main()
{
{
long myClassVar = 5;
}
myClassVar = 36;
}
}

It will not compile.....why? I do not need a "fix" but rather am confused
why this will not compile.
It gives an error at myClassVar=36 that is conflicts with declaration
Example1.myClassVar

Hi Afzal,

The compiler resolves the myClassVar identifier to the namespace
Example1.myClassVar and is looking for a class named myClassVar, for which
there is none. You shouldn't name namespace identifiers the same as types
or type members. Instead, use something like
Company.Product.SubSystem.Library as a general guideline when using
namespaces.

Joe
 
What is wrong with this?

class Example1
{
public static long myClassVar;

public static void Main()
{
{
long myClassVar = 5;
}
myClassVar = 36;
}
}

It will not compile.....why? I do not need a "fix" but rather am
confused why this will not compile.
It gives an error at myClassVar=36 that is conflicts with
declaration Example1.myClassVar

Afzal,

The documentation for error CS0135 states:

<QUOTE>
The compiler does not allow hiding names, which commonly leads to
logic errors in your code.
The following sample generates CS0135:
// CS0135.cs
public class MyClass2
{
public static int i = 0;

public static void Main()
{
{
int i = 4;
i++;
}
i = 0; // CS0135
}
}
</QUOTE>


Hope this helps.

Chris.
 
Hi Afzal,

The compiler resolves the myClassVar identifier to the namespace
Example1.myClassVar and is looking for a class named myClassVar,
for which there is none. You shouldn't name namespace
identifiers the same as types or type members. Instead, use
something like Company.Product.SubSystem.Library as a general
guideline when using namespaces.

Joe,

??? There's no namespace in his code example.


Chris.
 
Hi,
your problem is that the c# compiler does not like two variables with
the same name in the scope of the same function (here the myClassVar
variable in your main function) the static variable is also valid in
the same scope.

regards
peyman zehtab-fard
 
And the reason for this specific limitation is that it is intended to reduce
the incidence of programming errors.

Yes, it would be perfectly reasonable for the language to allow the
"redeclaration" of the variable in different bracket-scopes within the same
function, and various C-like languages do allow this. But the C# language
designers decided that the programming practice of redeclaring variables in
such a way was error prone, and specifically outlawed it.

A similar spirit is seen in the switch / case statement, where limits are
placed on how one case statement can "fall through" to the next case
statement. Accidental case fall through is a somewhat common error in
C-like languages, and the language C# language designers want to "protect"
programmers somewhat.

--Don
 
Back
Top