Adam Chalkley wrote:
Thanks that fixed the compiler error,but I thought you could assign/innitalize a variable anywhere within the method aslong as you declare it?
Keep in mind that the compiler operates at ... compile time...
The issue here is that at compile time, the compiler only sees calling a method on an instance (when looking at the condition of the while loop). It doesn't know if the condition is ever true. In order words, it thinks that it is possible for the while loop to have zero iterations.
So, yes, you can initialize the variable after to declared it, but you must guarantee that it will be initialized, with every possible path prior to its first usage (including code paths that can't actually happen at runtime).
Henry