switch, case, strings

  • Thread starter Thread starter Hilton
  • Start date Start date
H

Hilton

Hi,

It's great C# handles strings in a switch/case, but why do they have to be
constants? Heck, they could even be function calls etc, as long as they
return a string. If there are duplicates, first one wins. I don't see a
problem.

What am I missing?

Hilton
 
Anything is "possible" but everything takes time. Allowing non-const
strings wouldn't allow you to do anything that you can't do now, so what's
the big deal?

Paul T.
 
Paul said:
Anything is "possible" but everything takes time.

Wouldn't the implementation be exactly the same as it is now? i.e. the
compiler just breaks the switch into (the equivalent of) "if" statements
anyway. How about this idea... I write a Perl script to find switch
statements and convert them to "if/else" statements. Simple. I then have
the functionality. My argument is that the compiler already does this.
Right now it's just doing one additional check to ensure that the values are
constants - for no apparent reason to me.

Allowing non-const
strings wouldn't allow you to do anything that you can't do now, so what's
the big deal?

Well, that's an argument for getting rid of switch/case completely. It is
there to make things neat and more readable (not for added functionality),
which is why I'd like to use variables/expressions instead of just
constants.

I currently have a real example (hence the posting).

Hilton
 
It's the strings to which the input string is being compared which are
supposed to be constants. That is, it's the 'case' items, not the 'switch'
item...

Paul T.
 
Back
Top