Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

How to discard a value in X#? 14 Nov 2022 17:35 #24420

  • VR
  • VR's Avatar
  • Topic Author


  • Posts: 82
  • Hello

    when I want to convert a string to an int, I use Int.TryParse which returns true, when the string can be parsed and also has an out param with the string value.
    var str := "123"
    if int.TryParse(str, out var strAsInt)
      // do something with the value strAsInt
    endif

    But how can I handle the situation, when I only want to check, if the string can be parsed as Int,. To use TryParse, I have to define a variable for the out parameter and if I don't use it in the code, I'll get a warning.

    In C#, I would use _ to discard the value.
      static bool StringIsInt(string value) 
      {
         return int.TryParse(value, out _);
      }

    Is there a way to do the same in x# (2.13)

    Kind regards

    Volkmar

    Please Log in or Create an account to join the conversation.

    How to discard a value in X#? 14 Nov 2022 17:41 #24421

    • robert
    • robert's Avatar


  • Posts: 3213
  • Volkmar,
    Just like C#:
    if Int32.TryParse(str, out var _)

    we also allow
    if Int32.TryParse(str, out null)

    You can use the discard variable (_) everywhere just like in C#


    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    How to discard a value in X#? 14 Nov 2022 17:43 #24422

    • VR
    • VR's Avatar
    • Topic Author


  • Posts: 82
  • Thanks for the super fast response :-)

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1