Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the eror i'm gettting when i run the project

Error	1	'CreatingCheckBoxList.CheckBoXList.isCheckboxSelected()': not all code paths return a value	C:\Documents and Settings\cmsdev3\My Documents\Visual Studio 2005\Projects\CreatingCheckBoxList\CreatingCheckBoxList\CheckBoXList.aspx.cs	43	21	CreatingCheckBoxList

This i my code
C#
public bool isCheckboxSelected()
        {
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    return true;
                }
                else
                {

                    string.Format("you have to select each check box");
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            isCheckboxSelected();
        }
Posted
Updated 15-Aug-11 9:50am
v2
Comments
Wendelius 15-Aug-11 15:51pm    
Code blocks added

Add for example return false to the end of the method.

Another thing, where is the formatted string going? Currently nowhere. Also this method would end in the first selected item, is that what you want?

If you want to show for example a message box if every check box in the list isn't selected, could you simplify this to something like:
C#
public bool isCheckboxSelected()
        {
            bool noProblems;

            noProblems = CheckBoxList1.Items.Count == CheckBoxList1.SelectedItems.Count;
            if (!noProblems )
            {
               System.Windows.MessageBox.Show("you have to select each check box");
            }

            return noProblems ;
        }


[edit]Enhanced the code example to have only one exit point[/edit]
 
Share this answer
 
v2
Ignoring the fact that I'd do this a little differently, the general problem is that sometimes, the method (as you've written it) doesn't have enough returns to cover all possible paths of execution. To solve this, you should only have ONE exit point from a method, like so:

C#
public bool isCheckboxSelected()
{
    bool selected = false;
    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
    {
        if (CheckBoxList1.Items[i].Selected)
        {
            selected = true;
            break;
        }
    }
    if (!selected) 
    {
        string.Format("you have to select each check box");
    }

    // ONE exit point
    return selected;  
}
 
Share this answer
 
v3
Comments
Wendelius 15-Aug-11 16:01pm    
Good answer, my 5
Dr.Walt Fair, PE 15-Aug-11 18:41pm    
By way of explanation for the OP, in case that nothing is selected, there is no return value, hence the message.
BillWoodruff 15-Aug-11 19:19pm    
This solution would return true if any ListViewItem-with-a-CheckBox were Selected, and, while the OP's example is a bit ambiguous: the words "select each check box" in his orphan-string strongly suggests to me that he wants to evaluate if all ListViewItems are selected.
#realJSOP 16-Aug-11 7:29am    
In my own defense, I was merely attempting to get his code to compile (and did mention that I'd do it differently).
BillWoodruff 16-Aug-11 17:48pm    
Hi John, Point well-taken; didn't mean to nit-pick. My comment revised so that, hopefully, it's 'neutral.'
Assuming you are using a ListView here with ListViewItems exposing CheckBoxes:

0. I can't help wondering if you are really interested in whether the exposed CheckBoxes of each ListViewItem are Checked ... rather than selected.

Select "each CheckBox" ... your words ... is possibly ambiguous, because, what you select in a ListView is ListViewItems, not CheckBoxes: you check, or uncheck, a ListViewItem CheckBox.

1. what is your intent here: your code example shows a case of returning true if any ListViewItem-with-CheckBox is selected, but the text in your code clearly implies you want to return if every ListViewItem is selected.

2. Mika's given you the correct answer for finding if every ListViewItem is selected. And, that answer got my 5 !

3. So that leaves the issue of what you intend to happen when it is the case that one or more ListViewItems are not selected.

What do you want to do with that string you create: write it to a TextBox somewhere in the UI; or, put up a modal dialogue so the Application is suspended while the user is forced to acknowledge the condition ?

good luck, Bill
 
Share this answer
 
v4

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