Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi experts,

When debugging this code:
C#
enum MyEnum
{
    firstEnumEntry,
    secondEnumEntry
}

MyEnum _enumVariable = MyEnum.firstEnumEntry;

private void MyMethod()
{
    switch (_enumVariable)
    {
        case MyEnum.firstEnumEntry:
            _enumVariable = MyEnum.secondEnumEntry;
            MyMethod();
            break;
        case MyEnum.secondEnumEntry:
            DoSomeOtherThing();
            break;
        default:
            DoSomeDefaultThing();
            break;
    }
}

void DoSomething() { }
void DoSomeOtherThing() { }
void DoSomeDefaultThing() { }
void DoCoolStuff() { }
I came across the behaviour that execution stepped into case firstEnumEntry and run MyMethod() for the second time. When it got to switch(), it quit MyMethod(). No exception, no highlighted code line. The program just continued as if I had hit F5, except that the rest of the method did not execute.

What has happened here?
Am I to wrap a simple switch() in a try block? There's nothing in there that should be able to go wrong.

Ciao,


lukeer

[Edit]
According to Mahesh Bailwal's suggestion, I used a lot of breakpoints. Let's say the one in front of DoCoolStuff() was triggered. The method was called and worked but when I expected execution to return, same thing happened as described above.
DoCoolStuff()'s closing bracket was not highlighted and execution didn't return to MyMethod(). Instead the program continued just as if someone had hit F5.
[/Edit]
Posted
Updated 17-Jun-13 22:04pm
v3
Comments
Mahesh Bailwal 18-Jun-13 3:25am    
put break point at every case(including default) and check whether its breaking there or not.
lukeer 18-Jun-13 4:00am    
I tried and updated the question.
Richard MacCutchan 18-Jun-13 3:45am    
I could not even get the above to compile.
lukeer 18-Jun-13 4:05am    
I changed the example code.
Richard MacCutchan 18-Jun-13 4:19am    
I tried with your modified code and it works exactly as expected. I can only assume that you have something else in your code that you are not showing us.

1 solution

I tried your example code above as is (with Console.WriteLine in each method) and called it from a Button Click handler:
C#
private void button1_Click(object sender, EventArgs e)
    {
    MyMethod();
    }

enum MyEnum
    {
    firstEnumEntry,
    secondEnumEntry
    }

MyEnum _enumVariable = MyEnum.firstEnumEntry;

private void MyMethod()
    {
    switch (_enumVariable)
        {
        case MyEnum.firstEnumEntry:
            _enumVariable = MyEnum.secondEnumEntry;
            MyMethod();
            break;
        case MyEnum.secondEnumEntry:
            DoSomeOtherThing();
            break;
        default:
            DoSomeDefaultThing();
            break;
        }
    }

void DoSomething()
    {
    Console.WriteLine("void DoSomething()");
    }
void DoSomeOtherThing()
    {
    Console.WriteLine("void DoSomeOtherThing()");
    }
void DoSomeDefaultThing()
    {
    Console.WriteLine("void DoSomeDefaultThing()");
    }
void DoCoolStuff()
    {
    Console.WriteLine("void DoCoolStuff()");
    }
Ran without breakpoints: printed
void DoSomeOtherThing()
Ran again, with a breakpoint on the switch, and "step into" on each line:
Broke ok
Stepped into "firstEnumEntry"
Stepped into "MyMethod"
Stepped into "secondEnumEntry"
Stepped into "DoSomeOtherThing"
Printed "void DoSomeOtherThing()"
Stepped out of "MyMethod", back to "firstEnumEntry" break statement.
Stepped out of "MyMethod"

Which is what I would expect.
Basically, I didn't get any odd effects at all: what are you doing that is different from me? Could it be the contents of your "DoThis" methods?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900