473,320 Members | 1,951 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,320 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 1666
"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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.