Boolean.TryParse Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Updated: October 2010
Tries to convert the specified string representation of a logical value to its Boolean equivalent. A return value indicates whether the conversion succeeded or failed.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function TryParse ( _
value As String, _
<OutAttribute> ByRef result As Boolean _
) As Boolean
public static bool TryParse(
string value,
out bool result
)
Parameters
- value
Type: System.String
A string containing the value to convert.
- result
Type: System.Boolean%
When this method returns, if the conversion succeeded, contains true if value is equivalent to TrueString or false if value is equivalent to FalseString. If the conversion failed, contains false. The conversion fails if value is nulla null reference (Nothing in Visual Basic) or is not equivalent to either the TrueString or FalseString field.
Return Value
Type: System.Boolean
true if value was converted successfully; otherwise, false.
Remarks
The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.
The value parameter can be preceded or followed by white space. The comparison is case-insensitive.
Examples
The following example calls the TryParse method to parse an array of strings. Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim values() As String = { Nothing, String.Empty, "True", "False",
"true", "false", " true ", "0",
"1", "-1", "string" }
For Each value In values
Dim flag As Boolean
If Boolean.TryParse(value, flag) Then
outputBlock.Text += String.Format("'{0}' --> {1}", value, flag) & vbCrLf
Else
outputBlock.Text += String.Format("Unable to parse '{0}'.",
If(value Is Nothing, "<null>", value)) & vbCrLf
End If
Next
End Sub
End Module
' The example displays the following output:
' Unable to parse '<null>'.
' Unable to parse ''.
' 'True' --> True
' 'False' --> False
' 'true' --> True
' 'false' --> False
' ' true ' --> True
' Unable to parse '0'.
' Unable to parse '1'.
' Unable to parse '-1'.
' Unable to parse 'string'.
using System;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
string[] values = { null, String.Empty, "True", "False",
"true", "false", " true ", "0",
"1", "-1", "string" };
foreach (var value in values)
{
bool flag;
if (Boolean.TryParse(value, out flag))
outputBlock.Text += String.Format("'{0}' --> {1}", value, flag) + "\n";
else
outputBlock.Text += String.Format("Unable to parse '{0}'.",
value == null ? "<null>" : value) + "\n";
}
}
}
// The example displays the following output:
// Unable to parse '<null>'.
// Unable to parse ''.
// 'True' --> True
// 'False' --> False
// 'true' --> True
// 'false' --> False
// ' true ' --> True
// Unable to parse '0'.
// Unable to parse '1'.
// Unable to parse '-1'.
// Unable to parse 'string'.
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also
Reference
Other Resources
Change History
Date |
History |
Reason |
---|---|---|
October 2010 |
Replaced the example. |
Customer feedback. |