All -- I am refactoring some older code and I am looking at ways to reduce (or if not eliminate it altogether) the use of the GoTo statement. I have a section of code as follows:
public void GetData()
{
TryAgain:
Foo foo = bar.GetData();
if(foo == null)
{
bar.addItem("Test");
goto TryAgain;
}
//Use the bar object
}
Replacing it with the following:
public void GetData()
{
Foo foo = bar.GetData();
if(foo == null)
{
bar.addItem("Test");
GetData();
return;
}
//Use the bar object
}
Any thoughts or a better way to handle this?
UPDATE
First of all this isn't my actual code, I created this snippet for brevity purposes. Next please assume that once a value has been added to bar then the IF statement will be bypassed and the code section will continue and use the bar object. I want to create only one method that first checks to make sure that the bar object is not null and if it isn't then proceed runing the remainder of the code in the method. Sorry for the confusion.