Mike
Mike

Reputation: 391

should i always include else in my if statement?

I am getting an error message "not all code paths return a value". Can anybody tell me what I missed?

    public string AddLineBreak(string str, int column)
    {
        if (str == null || str.Length == 0)
           return "";
    }

Upvotes: 0

Views: 37

Answers (1)

Ken White
Ken White

Reputation: 125726

You missed what happens if the if isn't true.

public string AddLineBreak(string str, int column)
{
    if (str == null || str.Length == 0)
       return "";
    // What happens if str != null or str.Length != 0?
}

In this case, you can resolve it with a simple return (presuming you know what you want to return, that is):

public string AddLineBreak(string str, int column)
{
    if (str == null || str.Length == 0)
       return "";
    return WhatEver_AddLineBreak_Using_str_and_column_returns;
}

Upvotes: 1

Related Questions