2

I have the following code for my code but it always says the return statement is missing, even though I have put them in a switch list.

public IMap Map(string oldtheme) 
{ 
    switch (oldtheme) 
    { 
        case "archer": return new Archer(); 
        case "craftyblue": return new CraftyBlue(); 
        case "minimal": return new Minimal(); 
        case "mintalicious": return new Mintalicious(); 
        case "misfit": return new Misfit(); 
        case "peach": return new Peach(); 
        case "queen": return new Queen(); 
        case "sketch": return new Sketch(); 
        case "takeaway": return new TakeAwayLemonFresh(); 
        case "lemonfresh": return new TakeAwayLemonFresh(); 
        case "vanilla": return new Vanilla(); 
        case "velvet": return new Velvet(); 
        case "victoriana": return new Victoriana(); 
        case "writer": return new Writer();
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Zhobday
  • 31
  • 5

4 Answers4

4

You need to handle the case when oldtheme is none of the values you check for.

Depending on your case, I suggest throwing a ArgumentException so you know when it happens. I have added a default case to your switch statement:

public IMap Map(string oldtheme)
{
    switch ( oldtheme )
    {
        case "archer": return new Archer();
        case "craftyblue": return new CraftyBlue();
        case "minimal": return new Minimal();
        case "mintalicious": return new Mintalicious();
        case "misfit": return new Misfit();
        case "peach": return new Peach();
        case "queen": return new Queen();
        case "sketch": return new Sketch();
        case "takeaway": return new TakeAwayLemonFresh();
        case "lemonfresh": return new TakeAwayLemonFresh();
        case "vanilla": return new Vanilla();
        case "velvet": return new Velvet();
        case "victoriana": return new Victoriana();
        case "writer": return new Writer();
        default: throw new ArgumentException("unexpected value of oldtheme");
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
1

You lack a default value. Just add;

default: return null;

At the bottom of your switch and you'll be fine.

Dion V.
  • 2,090
  • 2
  • 14
  • 24
0

What do you want to return if the string is none of those you have in your switch? Your function is missing a return statement for that case.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

You have to use break after every return command and it will work fine.

  • Hi Andre, that's not true. The `break` keyword is only necessary if the control can continue to the next `case`, which it can't because the execution is `return`ed. See [switch statement is return suitable to replace break](http://stackoverflow.com/q/3196163/1364007), for instance for a discussion. – Wai Ha Lee Apr 01 '15 at 16:26