Share via


StringExtensions.AsBool Method (String, Boolean)

Converts a string to a Boolean (true/false) value and specifies a default value.

Namespace:  System.Web.WebPages
Assembly:  System.Web.WebPages (in System.Web.WebPages.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function AsBool ( _
    value As String, _
    defaultValue As Boolean _
) As Boolean
'Usage
Dim value As String 
Dim defaultValue As Boolean 
Dim returnValue As Boolean 

returnValue = value.AsBool(defaultValue)
public static bool AsBool(
    this string value,
    bool defaultValue
)
[ExtensionAttribute]
public:
static bool AsBool(
    String^ value, 
    bool defaultValue
)
static member AsBool : 
        value:string * 
        defaultValue:bool -> bool
public static function AsBool(
    value : String, 
    defaultValue : boolean
) : boolean

Parameters

  • defaultValue
    Type: System.Boolean
    The value to return if value is null reference (Nothing in Visual Basic) or is an invalid value.

Return Value

Type: System.Boolean
The converted value.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type String. When you use instance method syntax to call this method, omit the first parameter. For more information, see https://msdn.microsoft.com/en-us/library/bb384936(v=vs.111) or https://msdn.microsoft.com/en-us/library/bb383977(v=vs.111).

Remarks

For more information about how to use this method, see the AsBool() topic. If your code might receive as input an empty string (""), null reference (Nothing in Visual Basic), or a string value that cannot be converted to the specified type, you can use the AsBool overload to provide a default return value. For example, if you want to always return true by default in cases where value is invalid or null reference (Nothing in Visual Basic), you can use this method as shown in the example.

See Also

Reference

StringExtensions Class

AsBool Overload

System.Web.WebPages Namespace