Connecting Tech Pros Worldwide Forums | Help | Site Map

Switch Question

Craig S
Guest
 
Posts: n/a
#1: Nov 15 '05
I have something like this:

if (strVariable.BeginsWith("SOMETHING")
//whatever
if (strVariable.BeginsWith("ELSE")
//whatever
if (strVariable.BeginsWith("SomethingEvenLonger")
//whatever


Is there any way to put that in a case statement? The key to my trouble is
that the values I want to see if it begins with are all variable length so I
can't say switch (strVariable.SubString(0,10)) or similar... any ideas or do
I really have to do a bunch of 'ifs' ? (There are like 10-12 of them)

Or is there another more efficient approach?

Thanks in advance!
Craig



Jon Skeet
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Switch Question


Craig S <craig@removethis_birch.net> wrote:[color=blue]
> I have something like this:
>
> if (strVariable.BeginsWith("SOMETHING")
> //whatever
> if (strVariable.BeginsWith("ELSE")
> //whatever
> if (strVariable.BeginsWith("SomethingEvenLonger")
> //whatever
>
>
> Is there any way to put that in a case statement? The key to my trouble is
> that the values I want to see if it begins with are all variable length so I
> can't say switch (strVariable.SubString(0,10)) or similar... any ideas or do
> I really have to do a bunch of 'ifs' ? (There are like 10-12 of them)
>
> Or is there another more efficient approach?[/color]

Well, you could:

o Have a hashtable from leading substring to delegate, and iterate
through the table's keys, checking whether or not they're leading
substrings, and executing the delegate if not.

o Have a list of leading substrings, find the first that matches, and
then switch on that

o Use the ifs as above

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Closed Thread


Similar C# / C Sharp bytes