views:

237

answers:

7

I use this method to get file extension,

public string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".doc":
            case ".docx":
                return "application/ms-word";
        }
    }

When i compile it i got the error BaseClass.ReturnExtension(string)': not all code paths return a value.. Any suggestion...

+17  A: 

You need to add a default condition if you are returning from inside the switch statement.

// As SLaks mentioned, you should be case in-sensitive.
// Therefore, I'm comparing only the Lower Case version of the extensio

switch(fileExtension.ToLowerInvariant())
{
    case ".doc":
    case ".dox":
        return "application/ms-word";
    default:
        // You could also throw an Exception here if unknown extensions
        // are truly invalid (which seems to be the more popular choice)
        //
        // Since it looks like you're returning a content-type for a
        // download, I would default to octet-stream so the user would
        // just get a download window.
        return "application/octet-stream";
}
Justin Niessner
@Adrian: Wrong. An empy case can implicitly fall through.
SLaks
oh yes - i've never noticed that...
Adrian
+4  A: 

You need a default or you need to return something outside the switch.

default:
        Console.WriteLine("Default case");
        return "";
Michael B.
+16  A: 

You haven't specified what the method should return if fileExtension is not ".doc" or ".docx". You can do this by adding a default case to the switch statement. Assuming that other fileExtension values are invalid, you should throw an exception:

public string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".doc":
        case ".docx":
            return "application/ms-word";
        default:
            throw new ArgumentException(string.Format("Invalid fileExtension '{0}'.", fileExtension));
    }
}
Jamie Ide
exception is better than silent default +1
Rob Fonseca-Ensor
+1  A: 

The compiler derives a control flow graph from our source and sees that not all paths are covered, for example if you pass in a fileExtension ".rtf" this function can not provide a return value.

So either provide a return "something", at the end of the function or simply throw an exception for the switch's "default:" case. You have to decide about what the function needs to do in the cases when it neither sees a ".doc" or ".docx".

Armin
+3  A: 

This is one of the better error messages a compiler can issue. It means exactly that: Not all code paths return a value.

A code path is created by branching statements like while, if and switch.

The compiler is trying to guarantee, that whenever the runtime executes this function, a value will be returned of the specified type (here: string).

In your example, the return value for a fileextension not in the set (doc, docx) is not defined. You can either

  • specify a default clause in your switch statement
  • or add a catch-all return statement at the end of your method (return "text/plain"?)
Daren Thomas
+1  A: 

You need a default and break, or a break on your last case statement.

galford13x
No. There is no need for a break after a return.
Henk Holterman
A: 

Just my $0.10, it's pretty clear that the content header is being returned here, so if we don't recognize the file extension, rather than throw exceptions or invalid MIME header information (e.g., "unknown"), we should return the file to the browser as an octect stream, like this:

public string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".doc":
        case ".docx":
            return "application/ms-word";
        default:
            return "application/octet-stream";
    }
}

This way we can accommodate scenarios where 5 years down the road, M$ releases MS-Word v30.1, and the extension becomes ".docz", the system will not throw exceptions, but invoke MS-Word (more) gracefully, albeit not using the IE-enhanced behavior that "application/ms-word" will.

code4life