Switch Question

  • Thread starter Thread starter Craig S
  • Start date Start date
C

Craig S

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
 
Craig S said:
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?

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
 
Back
Top