473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Really weird 5 line scope-related problem

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
Nov 15 '05 #1
11 1668
"Afzal Mazhar" <af***@mazhar.net> wrote in message
news:fO**********************@news4.srv.hcvlny.cv. net...
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
--
http://www.csharp-station.com
Nov 15 '05 #2
"Afzal Mazhar" <af***@mazhar.net> wrote in message
news:fO**********************@news4.srv.hcvlny.cv. net...
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
--
http://www.csharp-station.com
Nov 15 '05 #3
"Afzal Mazhar" <af***@mazhar.net> wrote in
news:fO**********************@news4.srv.hcvlny.cv. net:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #4
"Afzal Mazhar" <af***@mazhar.net> wrote in
news:fO**********************@news4.srv.hcvlny.cv. net:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #5
"Joe Mayo" <jm***@ddiieessppaammeerrssddiiee.com> wrote in
news:em**************@tk2msftngp13.phx.gbl:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #6
"Joe Mayo" <jm***@ddiieessppaammeerrssddiiee.com> wrote in
news:em**************@tk2msftngp13.phx.gbl:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #7
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

"Afzal Mazhar" <af***@mazhar.net> wrote in message news:<fO**********************@news4.srv.hcvlny.cv .net>...
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

Nov 15 '05 #8
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

"Afzal Mazhar" <af***@mazhar.net> wrote in message news:<fO**********************@news4.srv.hcvlny.cv .net>...
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

Nov 15 '05 #9
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...

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


You're right. Thanks for the correction.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #10
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...

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


You're right. Thanks for the correction.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #11
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

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Peyman" <pe*****@netscape.net> wrote in message
news:69**************************@posting.google.c om...
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

"Afzal Mazhar" <af***@mazhar.net> wrote in message

news:<fO**********************@news4.srv.hcvlny.cv .net>...
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

Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: LRW | last post by:
I manage our mySQL database through putty (SSH terminal client). And whenever I do a select * from the table that contains ENCODEd passwords, the funky characters do funky things with the display....
2
by: Nils Emil P. Larsen | last post by:
Hello I have read about a C shared library which I want to use in my C program. (It's a library to encode/decode packets from/to a serial bus running with the SNAP-protocol). Unfortunatly...
1
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4...
13
by: Joe Attardi | last post by:
Over the weekend I attended a session on JavaScript at the No Fluff Just Stuff conference and learned an interesting quirk that I wanted to ask a question about.. Take this code: x = 5; ...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
5
by: robinsiebler | last post by:
I have a data structure that looks like this: # dates = {'2007': {'25': {'06/23/07': {'aerosmith': , # 'Metallica': }, # 'last_song': }}}...
0
by: Rory McKinley | last post by:
Hi I am trying to use the TidyHTMLTreeBuilder module which is part of elementtidy, but I am getting what appears to be some sort of scope error and it is scrambling my n00b brain. The module...
2
by: Matt Nordhoff | last post by:
Sebastjan Trepca wrote: Python doesn't like when you read a variable that exists in an outer scope, then try to assign to it in this scope. (When you do "a = b", "b" is processed first. In...
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.