Good news guys TryParse is coming for Objo’s scalar types in the next release: https://feedback.objo.dev/feature/924
I've now implemented TryParse methods for:
Integer
Double
Currency
DateTime
Each type provides two forms:
TryParse(text, result)
TryParse(text, locale, result)
The result is passed by reference. The method returns True when parsing succeeds and stores the parsed value in the result variable:
Var value As Integer
If Integer.TryParse("42", value) Then
Print(value) # 42
Else
Print("That is not a valid integer.")
End If
If parsing fails, the method returns False and resets the result to the type’s default value: zero for numeric values and an empty value for DateTime.
Locale-aware parsing is also supported:
Var french As Locale = Locale.FromIdentifier("fr-FR")
Var value As Double
If Double.TryParse("1,5", french, value) Then
Print(value) # 1.5
End If
I decided against adding a DisableCatch argument to Parse. Silently returning zero would make it impossible to distinguish invalid input from a legitimately parsed zero. TryParse keeps that distinction explicit without requiring a Try/Catch block.
The existing Parse methods have not changed: they still throw an InvalidArgumentException when parsing fails. In general:
- Use
Parse when invalid input represents an exceptional condition.
- Use
TryParse when invalid input is expected and needs validation, such as text entered by a user.
The compiler and VM were also updated so the Boolean result can be used directly inside another expression while still updating the referenced variable:
Var value As Integer
Print(Integer.TryParse("42", value))
Print(value)
Regarding making Throw information more visually prominent in the documentation has also been fixed in the next release of the docs.