What if an IEditableTextControl is a value not a string?

For example, if only a port number is valid, like in my example for OSC , what would be better than the following which crashes inside std::stoi if the user enters something that isn’t number like

nelosc.sender->changeTargetPort( std::stoi( pCaller->As<IEditableTextControl>()->GetStr() ) );

ok, well I found this solution on SE and it worked for me

  convert C strings to unsigned ints
  https://stackoverflow.com/questions/4024806/how-to-convert-from-const-char-to-unsigned-int-c#4024839
  */
 
 unsigned long cstring_to_ul(const char* str, char** end = nullptr, int base = 10)
 {
     errno = 0; // Used to see if there was success or failure
     auto ul = strtoul(str, end, base);
     if(errno != ERANGE)
     {
        return ul;
     }
     return ULONG_MAX;
 }
1 Like