why do I get compile error


T

Tony Johansson

Hi!

This first example works
static void Main(string[] args)
{
int j;
int i;
for (i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}

but this second example get compile error saying se below
static void Main(string[] args)
{
int j;
//int i;
for (int i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}
Error 1 A local variable named 'j' cannot be declared in this scope because
it would give a different meaning to 'j', which is already used in a 'parent
or current' scope to denote something else
F:\C#\ConsoleApplication1\Report.cs 18 26 ConsoleApplication1

I don't understand why my second example doesn't work when the first example
worked ?

//Tony
 
Ad

Advertisements

J

Jeff Johnson

Tony Johansson said:
Hi!

This first example works
static void Main(string[] args)
{
int j;
int i;
for (i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}

but this second example get compile error saying se below
static void Main(string[] args)
{
int j;
//int i;
for (int i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}
Error 1 A local variable named 'j' cannot be declared in this scope
because it would give a different meaning to 'j', which is already used in
a 'parent or current' scope to denote something else
F:\C#\ConsoleApplication1\Report.cs 18 26 ConsoleApplication1

I don't understand why my second example doesn't work when the first
example worked ?

The use of the int keyword inside the for () loop tells the compiler you're
trying to create a new variable. With multiple variables separated by a
comma, you can't just apply the type declaration to one of them; it applies
to all of them.
 
Ad

Advertisements

G

Göran Andersson

Jeff said:
Tony Johansson said:
Hi!

This first example works
static void Main(string[] args)
{
int j;
int i;
for (i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}

but this second example get compile error saying se below
static void Main(string[] args)
{
int j;
//int i;
for (int i = 0, j = 1; i <= j; i++,j -=2 )
Console.WriteLine("i= {0} j={1}", i, j);
}
Error 1 A local variable named 'j' cannot be declared in this scope
because it would give a different meaning to 'j', which is already used in
a 'parent or current' scope to denote something else
F:\C#\ConsoleApplication1\Report.cs 18 26 ConsoleApplication1

I don't understand why my second example doesn't work when the first
example worked ?

The use of the int keyword inside the for () loop tells the compiler you're
trying to create a new variable. With multiple variables separated by a
comma, you can't just apply the type declaration to one of them; it applies
to all of them.

The declaration of the variables in the for statement works just as it
would work outside it.

The code:

int i = 0, j = 1;

is not the same as:

int i = 0;
j = 1;

it's the same as:

int i = 0;
int j = 1;

You are limited by the fact that you can only have one initialisation
statement in the for statement, so you can't have one declaration and
one assignment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top