smr5
smr5

Reputation: 2793

Boolean Function c# not all code return a value

What am I doing wrong here?

At the end of the function, I'm returning the result.

 public bool  isStipends() 
    {
        try
        {
            bool result = true;
            if (tbstipend.Text == "" || tbstipend.Text == "Required")
            {
                tbstipend.Text = "Required";
                tbstipend.BackColor = Color.Red;
                tbstipend.ForeColor = Color.White;
                result =  false;
            }
            else if  (tbstipendperperiod.Text == "" || tbstipendperperiod.Text == "Required")
            {
                tbstipendperperiod.Text = "Required";
                tbstipendperperiod.BackColor = Color.Red;
                tbstipendperperiod.ForeColor = Color.White;
                result = false;
            }

            else  if (tbstipendsperinterval.Text == "" || tbstipendsperinterval.Text == "Required")
            {
                tbstipendsperinterval.Text = "Required";
                tbstipendsperinterval.BackColor = Color.Red;
                tbstipendsperinterval.ForeColor = Color.White;
                result = false;
            }

            else if (tbstipendrate.Text == "" || tbstipendrate.Text == "Required")
            {
                tbstipendrate.Text = "Required";
                tbstipendrate.BackColor = Color.Red;
                tbstipendrate.ForeColor = Color.White;
                result =  false;
            }
            else
            {
                return result;
            }

        }
        catch 
          {
             return false;
          }
    }

In the code behind of the button, I call:

 private void btnupdatestipends_Click(object sender, EventArgs e)
    {
        try
        {
            if (isStipends() == true)
            {
                MessageBox.Show("TEST");
            }
        }
        catch { }
    }

However, it gives me an error on the function itself.

Error 3 'AddressBookMaint.Form1.isStipends()': not all code paths return a value C:\Win\AddressBookMaint\AddressBookMaint\Form1.cs 5040 22 AddressBookMaint

Any suggestions?

Thank you.

Upvotes: 3

Views: 3869

Answers (6)

Mikael Dúi Bolinder
Mikael Dúi Bolinder

Reputation: 2294

Error 1

You are only returning if none of the if's are true since the `return` is in the last `else` clause.

Solution

Break out the `return` from the last `else` and place it in the `try` block *(or even outside of the try/catch and you will solve error 2 as well)*.

Error 2

You will only return if there are no exceptions since you have the `return` at the end of the `try` block and no `return` in the `catch` block.

Solution

Add a `return` in the `catch` block and the code will compile.

Here's a working version of your code

public bool  isStipends() 
{
    bool result = true;
    try
    {
        
        if (tbstipend.Text == "" || tbstipend.Text == "Required")
        {
            tbstipend.Text = "Required";
            tbstipend.BackColor = Color.Red;
            tbstipend.ForeColor = Color.White;
            result =  false;
        }
        else if  (tbstipendperperiod.Text == "" || tbstipendperperiod.Text == "Required")
        {
            tbstipendperperiod.Text = "Required";
            tbstipendperperiod.BackColor = Color.Red;
            tbstipendperperiod.ForeColor = Color.White;
            result = false;
        }

        else  if (tbstipendsperinterval.Text == "" || tbstipendsperinterval.Text == "Required")
        {
            tbstipendsperinterval.Text = "Required";
            tbstipendsperinterval.BackColor = Color.Red;
            tbstipendsperinterval.ForeColor = Color.White;
            result = false;
        }

        else if (tbstipendrate.Text == "" || tbstipendrate.Text == "Required")
        {
            tbstipendrate.Text = "Required";
            tbstipendrate.BackColor = Color.Red;
            tbstipendrate.ForeColor = Color.White;
            result =  false;
        }
    }
    catch 
    {
        result = false;
    }
    
    return result;
}

Upvotes: 4

Syed Farjad Zia Zaidi
Syed Farjad Zia Zaidi

Reputation: 3360

You are not returning anything anywhere in your code... except for your last else statement. To compile all paths should return a bool value for your method. You should return a value in every if or if else and else statement, and in your catch block.

This will work:

public bool  isStipends() 
{
    try
    {
        bool result = true;
        if (tbstipend.Text == "" || tbstipend.Text == "Required")
        {
            tbstipend.Text = "Required";
            tbstipend.BackColor = Color.Red;
            tbstipend.ForeColor = Color.White;
            result =  false;
        }
        else if  (tbstipendperperiod.Text == "" || tbstipendperperiod.Text == "Required")
        {
            tbstipendperperiod.Text = "Required";
            tbstipendperperiod.BackColor = Color.Red;
            tbstipendperperiod.ForeColor = Color.White;
            result = false;
        }
        else  if (tbstipendsperinterval.Text == "" || tbstipendsperinterval.Text == "Required")
        {
            tbstipendsperinterval.Text = "Required";
            tbstipendsperinterval.BackColor = Color.Red;
            tbstipendsperinterval.ForeColor = Color.White;
            result = false;
        }
        else if (tbstipendrate.Text == "" || tbstipendrate.Text == "Required")
        {
            tbstipendrate.Text = "Required";
            tbstipendrate.BackColor = Color.Red;
            tbstipendrate.ForeColor = Color.White;
            result =  false;
        }
        else
        {
            return result;
        }
    }
    catch 
    {
        return false;
    }
    return result;
}

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149068

Your code only specifies the return in the final else block. In all your other code paths, including the catch block, you haven't specified any return value. You can drop that final else block and add a return value at the end of your function, like this:

public bool  isStipends() 
{
    bool result = true;
    try
    {
        ...
    }
    catch
    {
        result = false;
    }

    return result;
}

However, catching all exceptions like this is very bad practice, and you certainly don't need to do it inside every function. You should only catch the exceptions you can meaningfully handle and allow the rest to bubble up. Set a global unhandled exception if need be to gracefully bail out of your application.

See Best Practices for Exceptions

Upvotes: 1

Lamourou abdallah
Lamourou abdallah

Reputation: 354

replace your method isStipends by this on:

public bool  isStipends() 
{
    try
    {
        bool result = true;
        if (tbstipend.Text == "" || tbstipend.Text == "Required")
        {
            tbstipend.Text = "Required";
            tbstipend.BackColor = Color.Red;
            tbstipend.ForeColor = Color.White;
            result =  false;
        }
        else if  (tbstipendperperiod.Text == "" || tbstipendperperiod.Text == "Required")
        {
            tbstipendperperiod.Text = "Required";
            tbstipendperperiod.BackColor = Color.Red;
            tbstipendperperiod.ForeColor = Color.White;
            result = false;
        }

        else  if (tbstipendsperinterval.Text == "" || tbstipendsperinterval.Text == "Required")
        {
            tbstipendsperinterval.Text = "Required";
            tbstipendsperinterval.BackColor = Color.Red;
            tbstipendsperinterval.ForeColor = Color.White;
            result = false;
        }

        else if (tbstipendrate.Text == "" || tbstipendrate.Text == "Required")
        {
            tbstipendrate.Text = "Required";
            tbstipendrate.BackColor = Color.Red;
            tbstipendrate.ForeColor = Color.White;
            result =  false;
        }
        return result;

    }
    catch { }
}

Upvotes: 1

Marztres
Marztres

Reputation: 470

The error is telling you that there are ways that do not return any value, if there is a mistake your catch instruction will returns nothing.

returns some value in your instruction catch.

Upvotes: 0

Neil Smith
Neil Smith

Reputation: 2565

You either need to return something in your catch:

public bool Method() {
    try {
        return true;
    }
    catch {
        return false;
    }
}

Or just return a single value at the bottom:

public bool Method() {
    bool result = false;
    try {
        ...
        result = true;
    }
    catch {}

    return result;
}

Upvotes: 2

Related Questions