I’m getting a compiler error on the following code (snippet). Why is this code incorrect? an solution
protected Dialog onCreateDialog(int paramInt)
{
switch (paramInt)
{
default:
case 0:
}
for (Object localObject = null; ; localObject = this.dialog)
{
return localObject; // here problem cast
this.dialog = new ProgressDialog(this);
this.dialog.setMessage(getResources().getString(2131165201));
this.dialog.setIndeterminate(true);
this.dialog.setCancelable(false);
}
}
You are having
returnstatement beforethis.dialog = new ProgressDialog(this);which becomesunreachablecode because control will never reach to the next direct statement after return statement. This will result into compilation error. You need to flip the order as:I am not sure what your loop will do but one thing is sure that it will not loop but simply return in first iteration itself. Also your
localObjectwill remainnullas it will not reach theincrementblock of theforloop(it returns beforehand because of return statement).EDIT: Just to fix your compilation error, move your return statement in the bottom of the
loopas:As I mentioned earlier, I am not getting the real reason of using the
forloop as it’s not going to loop at all because of thereturnstatement inside.