When i write case statements my last line is always:

default:
throw new Exception("Unknown selection");

how can I change this so a message comes up for the user to do something with the data he or she inputted so the default will not occur.

Recommended Answers

All 6 Replies

By doing just what you described.

What knowledge are you lacking that is preventing you from implementing this behavior? (Bringing up a message? Something else?)

I tried a console.write ("instructions"); break;
but nothing comes up when I test it.

You can't give dynamic statement as it's your logic exception, the method calls this method (which contains this switch...case) will handle this throw with meaningful error message to the user.
I really don't understand your question, give a scenario.

Here is my code. Can you help see what to change. Im sorry but Im lost. Basically I want a message to come up if this code fails because a proper variable is not imputted.

        decimal blankl;
        decimal blankw;            
        decimal.TryParse(blanklTB.Text,out blankl);
        decimal.TryParse(blankwTB.Text, out blankw);

        if (bunitsCB.Text == "English")
            {                
                switch (dieunitsCB.Text)
                {
                    case "English":
                        dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(24));
                        diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(24)); break;
                    case "Metric":
                        dielTB.Text = Convert.ToString(Math.Round((blankl * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588));
                        diewTB.Text = Convert.ToString(Math.Round((blankw * Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(588)); break;
                    default:
                        throw new Exception("Unknown selection");
                }
             }
            else
            {
                switch (dieunitsCB.Text)
                {
                    case "English":
                        dielTB.Text = Convert.ToString(Math.Round((blankl / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24));
                        diewTB.Text = Convert.ToString(Math.Round((blankw / Convert.ToDecimal(25.4)), 0) + Convert.ToDecimal(24)); break;                           
                    case "Metric":                            
                        dielTB.Text = Convert.ToString(blankl + Convert.ToDecimal(588));
                        diewTB.Text = Convert.ToString(blankw + Convert.ToDecimal(588)); break;
                    default:
                        throw new Exception("Unknown selection");
                }

Please use code tags - that code is hard to read correctly without - and its so simple to put them in.

OK, your code is unclear if nothing else because you have a test to see if dunitsCB.Text says english or not..

You then switch on dieunitsCB.Text or dieunitsCB.Text (eg the same thing) to produce different answers. Its confusing as you have 3 different tests for "English" making it confusing.

So, your problem seems to be you want to complain if neither English or Metric arent selected - why go through all that hassle, why not just test that this is not the case and "do something"

Seeing how this is an extension of the thread you started: http://www.daniweb.com/forums/thread176030.html

Where I provided you with the exception on the default of "unknown selection" but you didn't update the original thread or mark it solved i'm not seeing a good reason to continue to help...

Rashakil Fol gave you the answer you're looking for, though...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.