Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey All
I am returning text box from class but when i use while loop and return textbox within loop, i am getting error as follows
not all code paths return a value
need help

What I have tried:

C#
public class FormDesign
{
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;
        }
                                                                                                                                    }

}
Posted
Updated 19-Sep-20 19:33pm

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:
C#
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:
C#
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.
 
Share this answer
 
v2
So, follow the code. What happens when the code drops out of the while loop when i is equal to 3? That's what the error is talking about.
 
Share this answer
 
i figured it out myself
if you want to return multiple values using loop from function then you will have to create an array function instead of normal function
C#
public TextBox[] txtbox(int count)
        {
                                    TextBox[] txtbox;
            txtbox = new TextBox[count];
            int i = 0;
            while(i<txtbox.Length)
                        {
                TextBox t = new TextBox();
                txtbox[i] = t;
                txtbox[i].Text= "inserted textbox array"+i.ToString();
                mypanel.Controls.Add(txtbox[i]);
                i++;
            }
            return txtbox;
        }
 
Share this answer
 
Comments
Richard MacCutchan 20-Sep-20 3:19am    
Which is exactly what OriginalGriff told you yesterday.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900