Switch Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig S

    #1

    Switch Question

    I have something like this:

    if (strVariable.Be ginsWith("SOMET HING")
    //whatever
    if (strVariable.Be ginsWith("ELSE" )
    //whatever
    if (strVariable.Be ginsWith("Somet hingEvenLonger" )
    //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.Su bString(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

    #2
    Re: Switch Question

    Craig S <craig@removeth is_birch.net> wrote:[color=blue]
    > I have something like this:
    >
    > if (strVariable.Be ginsWith("SOMET HING")
    > //whatever
    > if (strVariable.Be ginsWith("ELSE" )
    > //whatever
    > if (strVariable.Be ginsWith("Somet hingEvenLonger" )
    > //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.Su bString(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.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...