C++/CLI, Strings and switch()

Under C# it is possible to feed strings to the switch() statement and its cases, this doesn't seem to work in C++/CLI. Is there a way to make it work, which I don't know about, or don't I get around chaining if-statements

Thanks.


Answer this question

C++/CLI, Strings and switch()

  • citifortune concepts

    Sorry when I said 'you could probably do the same in C++/CLI', I meant you achieve the effect by using a Hashtable or if statements.

  • Sree Prakash

    This behavior is not achievable with C++ (native or managed). Your only recourse is to use if statements.

    Sorry,

    --
    Boris Jabes, Visual C++ Team
    This post is provided "AS IS" and confers no rights

  • eric963258741

    Don't need the /CLI part, even with unmanaged code you could do something
    like

    std::map< std:Tongue Tiedtring, boost::function<void()> > strswitch;
    strswitch["Foo"] = &foo;
    strswitch["Bar"]  =  bind(MyBarObj, &CMyBar:Big Smileoit );
    ...
    strswitch[ GetArg( )  ] ( );

    If you want to get really fancy, you can wrap this in a class so you won't need
    the trailing ( ) in strswitch[ GetArg( )  ] ( ), and/or pass the string argument to
    the called function if a map entry is initialized with a boost::function<void(string)>

    Without boost, you could use a void(*)( ) in the map.

    Regards,
    Michiel Salters

  • Holywhippet

    The C# compiler actually converts the switch statement to either a Hashtable lookup or if there are not many values, to a few if statements.

    So you could probably do the same in C++/CLI.


  • C++/CLI, Strings and switch()