Anonymous554445

oddly( )... when you attempt multiple initiator or iterator statements within a for loop

it works (for initiators) only if there is only a single type of variable declared

ie the following works

for(int x=0, y=0; y<2;y++,x++)

even though y is not declared elsewhere (the compiler assumes this is simply a multiple declaration of type int, perhaps not correct if y is actually previously declared as a different type )

but the following generates a compiler error

for (int x=0, ushort y=0; y<3; y++, x++)

because the compiler is looking for a variable name instead of the type ushort

I suspect the compiler is really treating this as a single initlizer (ie you could do this on one statement outside the foor loop if the types were all the same)

so... when you write for loops with multiple initializer statements you seemingly can not use previously undeclared variables of different types



Re: Visual C# General C# for loop multiple init

IsshouFuuraibou

That is correct.

remember that each of the options in the for line is the same as doing it on it's own line.

you can't just do

int x = 0, ushort y = 0;

even outside of the for statement, it's part of the language specification.

if the issue is of sizes and lifetime of the variable, you can always use the using statement

using ( ushort y = 0 ){

for( int x = 0; y < 3; x++, y++ ){

}

}






Re: Visual C# General C# for loop multiple init

Anonymous

not _entirely_ sure I understand

you can't do x++,y++ on its own line either, but you certainly can within a for-loop iterator part

and I am pretty sure (have not tried it) that you can similarly use two entirely unrelated statements in the initializer part

for (doThis(), doThat(); x < 3; x++, y++);

whereas you could not have a seperate line

doThis(), doThat();

is that more or less correct

so I am not sure I get your answer here





Re: Visual C# General C# for loop multiple init

IsshouFuuraibou

The for statement clauses are unique,

however initialization still follows the restriction of initializing in a list with comma separation.

In other words, the for rules defaults to standard rules for statements separated by commas unless there isn't one, then it performs the statements in order. Since there is a convention for int x = 0, y = 10, z = 123... that is what governs the use inside the for clause. There is no convention for MethodA(), MethodB() therefore the for clause uses its special convention for it.

It's a case with declarations that there is an existing convention that they code specification doesn't want to change. I'm sure if the developers wanted to they could have done single line multi declarations differently.





Re: Visual C# General C# for loop multiple init

Anonymous

Of course, there is no standard rule / convention for

int x = 0, ushort y = 0

either, right But rather than use the special convention for for-statement initiators... the compiler generates an error treating them as a single statement rather than a complex set of comma-seperated for-loop statements

So is it a language spec issue or is it simply a compiler issue

Anyhow, thanks for the help.





Re: Visual C# General C# for loop multiple init

swprogrammer

(for some reason my posts on this thread, including the one which started it, were removed weird)

this turned out not to be the case at all, according to the c# language spec.

it has nothing to do with "It's a case with declarations that there is an existing convention that they code specification doesn't want to change. I'm sure if the developers wanted to they could have done single line multi declarations differently.
"

the language spec states simply in a for-loop for the initiator(s) you can include EITHER a SINGLE declaration-statement (not the correct jargon, but you get the point), which like any declaration may include an initialization, OR MULTIPLE evaluation-statements, which do not include variable declarations.

so

for (byte x=0, y=0; x< 2; x++, y++)

is legal since "byte x=0, y=0" is a single declaration-statement for multiple variables including initializations

but effectively you can never declare varaiables of more than one type in the for-loop initiator because it would require multiple declaration statements.

meaning even

int x;

for (x=0, int y=0; x < 2; x++, y++)

is illegal since you are combining a declaration with a normal evaluation statment in the for-loop initiator

however you could use multiple initializations of previously-declared variables.

just interesting