473,405 Members | 2,272 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,405 software developers and data experts.

Why Do I Get a Compile Error Here?

My class has a member variable declared as:

string address;

Here is a method of my class:

/// <remarks>
/// This is run in a seperate thread.
/// </remarks>
void DoListen()
{
1: listener = new TcpListener(IPAddress.Parse(address), port);
2: listener.Start();
3: for (;;) {
4: Socket client = listener.AcceptSocket();
5: string address = (client.RemoteEndPoint as IPEndPoint).Address.ToString();
6: Connection connection = new Connection(client);
7: }
}

I get a compile error on line 5:

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

Am I missing something, or have I encountered a C# compiler bug?
Nov 16 '05 #1
3 1063
C# Learner wrote:
[...]


Oh, I understand now. The following reproduces it:

void FooBar()
{
address = "foo";
if (true) {
string address = "bar";
}
}

I guess the underscore advocates will have a field day now. :-P
Nov 16 '05 #2
In line one you have "address" and in line 5 you are declaring "address" a
second time. You either need to remove "string" from in front of the second
"address" or change the second "address" to another name.

Regards,
John
"C# Learner" <cs****@learner.here> wrote in message
news:Ow*************@TK2MSFTNGP10.phx.gbl...
My class has a member variable declared as:

string address;

Here is a method of my class:

/// <remarks>
/// This is run in a seperate thread.
/// </remarks>
void DoListen()
{
1: listener = new TcpListener(IPAddress.Parse(address), port);
2: listener.Start();
3: for (;;) {
4: Socket client = listener.AcceptSocket();
5: string address = (client.RemoteEndPoint as IPEndPoint).Address.ToString(); 6: Connection connection = new Connection(client);
7: }
}

I get a compile error on line 5:

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

Am I missing something, or have I encountered a C# compiler bug?

Nov 16 '05 #3
C# Learner wrote:
[...]
void FooBar()
{
address = "foo";
if (true) {
string address = "bar";
}
}
[...]


Or, even simpler:

void FooBar()
{
address = "foo";
string address = "bar";
}

Also note that it's no longer an error if 'this' is used in the first statement.
Nov 16 '05 #4

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

Similar topics

5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
10
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I...
6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
0
by: Jérôme Le Bougeant | last post by:
Hello (and sorry for my English), I downloaded the VideoCapture module on the http://videocapture.sourceforge.net/ site. I tested it with a webcam and that functions. Now I want to...
5
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
2
myusernotyours
by: myusernotyours | last post by:
Hi All, Am working on a Java application in which I have to use the JNI to Interface with some native code for both windows and unix. Am using netbeans IDE with the C/C++ pack installed. Am also...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.