The error means what it says: there is at least one route through the method that could result in nothing being returned.
In practice, it can't happen, but ... if I fix your indentation:
public TextBox txtbox(string textid)
{
TextBox txt = new TextBox();
int i = 1;
while (i<3)
{
txt.ID = textid+i.ToString();
txt.Text = "inserted from class";
return txt;
}
}
The system doesn't work out that
i
will always be less than
3
, so it assumes that there are cases when the loop is not entered at all, and in that case there is no return value.
But ... you can't return multiple values, and if you use
return
inside a loop, the method exits immediately and does no more - so your loop (which would never exit if it wasn't for the
return
statement is pretty much pointless as your whole method evaluates to this:
public TextBox txtbox(string textid)
{
TextBox txt = new TextBox();
txt.ID = textid + "1";
txt.Text = "inserted from class";
return txt;
}
If you want to return multiple values, you need to change the method signature so it returns a collection : a
TextBox[]
or a
List<TextBox>
perhaps, and then assemble the collection before you exit.