System.StackOverflowException in switch cases

I have 12 Switch cases but it is executing only 10 cases . if add 11th case throwing exception "System.StackOverflowException".Please find the below code for your reference

 public enum EnumSearch
        {
            test1,
            test2,
          test3,
            test4,
            test5,
            test6,
            test7,            
            test8,
            test9,
            test10,            
            test11,                              
          test12

        }

 public string GetSearchResult(string searchCriteria)
        {
           

                // here  converting string search criteria to enum
                EnumManager.EnumSearch searchValue;
                EnumManager.EnumSearch.TryParse(searchCriteria, out searchValue);
               
               switch (searchValue)
                            {
                                case EnumSearch.test1:
                                                 //  some coding
                                         break;

                                case EnumSearch.test2:
                                                 //  some coding
                                         break;

                                case EnumSearch.test3:
                                                 //  some coding
                                         break;

                                case EnumSearch.test4:
                                                 //  some coding
                                         break;

                                case EnumSearch.test5:
                                                 //  some coding
                                         break;

                                case EnumSearch.test6:
                                                 //  some coding
                                         break;

                                case EnumSearch.test7:
                                                 //  some coding
                                         break;

                                case EnumSearch.test8:
                                                 //  some coding
                                         break;

                                case EnumSearch.test9:
                                                 //  some coding
                                         break;

                                case EnumSearch.test10:
                                                 //  some coding
                                         break;

                                case EnumSearch.test11:
                                                 //  some coding
                                         break;

                                case EnumSearch.test12:
                                                 //  some coding
                                         break;

}
}

In above code i have written 12 switch cases then it is throwing exception System.StackOverflowException.

suppose In above switch,If i write any 10 cases (means remove any 2 cases ) then it is working fine with out any exception

Could you please help me regarding this

Thanks In Advance...
ganeahdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

I don't see any problem here.

what is the value here

searchCriteria = ?
0
ste5anSenior DeveloperCommented:
Please post a concise and complete example, which shows this behavior. Cause a simple program according to your description works for me:

namespace ConsoleCS
{
    using System;

    class Program
    {
        enum EnumSearch
        {
            test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12
        }

        static string GetSearchResult(string searchCriteria)
        {
            EnumSearch searchValue;
            EnumSearch.TryParse(searchCriteria, out searchValue);
            switch (searchValue)
            {
                case EnumSearch.test1: return "test1"; break;
                case EnumSearch.test2: return "test2"; break;
                case EnumSearch.test3: return "test3"; break;
                case EnumSearch.test4: return "test4"; break;
                case EnumSearch.test5: return "test5"; break;
                case EnumSearch.test6: return "test6"; break;
                case EnumSearch.test7: return "test7"; break;
                case EnumSearch.test8: return "test8"; break;
                case EnumSearch.test9: return "test9"; break;
                case EnumSearch.test10: return "test10"; break;
                case EnumSearch.test11: return "test11"; break;
                case EnumSearch.test12: return "test12"; break;
                default: return "default"; break;
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine(GetSearchResult(""));
            Console.WriteLine(GetSearchResult("test1"));
            Console.WriteLine(GetSearchResult("test2"));
            Console.WriteLine(GetSearchResult("test3"));
            Console.WriteLine(GetSearchResult("test4"));
            Console.WriteLine(GetSearchResult("test5"));
            Console.WriteLine(GetSearchResult("test6"));
            Console.WriteLine(GetSearchResult("test7"));
            Console.WriteLine(GetSearchResult("test8"));
            Console.WriteLine(GetSearchResult("test9"));
            Console.WriteLine(GetSearchResult("test10"));
            Console.WriteLine(GetSearchResult("test11"));
            Console.WriteLine(GetSearchResult("test12"));
            Console.WriteLine(GetSearchResult("test13"));
            Console.WriteLine(GetSearchResult(null));
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window


Stack overflow indicates, that you may have a recursion in one of your // some coding areas,

btw, what are you trying to do with that enum and switch? It seems to have a code smell.
0
Lokesh B RDeveloperCommented:
Hi,

This code works perfect for me.

can you post the error screen?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AndyAinscowFreelance programmer / ConsultantCommented:
If ANY of the code you do not show calls this function (GetSearchResult) then you will get that error.  It is an infinite loop.
0
MlandaTCommented:
The issue is probably with what is happening in those last few cases. Examine the StackTrace on the exception, it will tell you where things are failing. As Andy suggests, it probably has to do with an infinite loop somewhere. Inspecting your stacktrace will pin point the faulty code.
0
sarabandeCommented:
Andy is right. a stack overflow comes if you programmed an infite loop by calling a function recursively with not an assured end. to verify that this is right you may change in the debug - exception settings that the debugger should break immediately when the System.StackOverflowException was thrown. if you do so and run your program again, examine the stack when the exception occured. you should see the function (or function sequence) which causes the stack overflow.

Sara
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MlandaTCommented:
It would be interesting (and helpful) to know what it was :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.