Boolean.TryParse メソッドとは? わかりやすく解説

Boolean.TryParse メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

論理値指定した文字列形式等価Boolean変換します戻り値は、変換成功した失敗したかを示します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function TryParse
 ( _
    value As String, _
    <OutAttribute> ByRef result As Boolean
 _
) As Boolean
public:
static bool TryParse (
    String^ value, 
    [OutAttribute] bool% result
)
public static boolean TryParse (
    String value, 
    /** @attribute OutAttribute() */ /** @ref */ boolean result
)
JScript では、値型引数参照渡しされません。

パラメータ

value

変換する値が含まれている文字列。

result

変換成功した場合、このメソッド返されるときに、value が TrueString と等価場合は、true格納しますvalue が FalseString と等価場合は、false格納します変換失敗した場合は、false格納しますvaluenull 参照 (Visual Basic では Nothing) であるか、TrueString または FalseString等価ない場合変換失敗します。このパラメータ初期化せずに渡されます。

戻り値
value正常に変換され場合trueそれ以外場合false

解説解説
使用例使用例

複数基本型対すTryParse メソッドオーバーロード、および TryParseExact メソッドコード例次に示します

' This example demonstrates overloads of the TryParse method for
' several base types, and the TryParseExact method for DateTime.
' In most cases, this example uses the most complex overload; that is,
 the overload 
' with the most parameters for a particular type. If a complex overload
 specifies 
' null (Nothing in Visual Basic) for the IFormatProvider parameter,
 formatting 
' information is obtained from the culture associated with the current
 thread. 
' If a complex overload specifies the style parameter, the parameter
 value is 
' the default value used by the equivalent simple overload.

Imports System
Imports System.Globalization

Class Sample
   Public Shared Sub Main()
      Dim result As Boolean
      Dim ci As CultureInfo
      Dim nl As String =
 Environment.NewLine
      Dim msg1 As String
 = _
                 "This example demonstrates overloads of the TryParse
 method for{0}" & _
                 "several base types, as well as the TryParseExact
 method for DateTime.{0}"
      Dim msg2 As String
 = "Non-numeric types:{0}"
      Dim msg3 As String
 = "{0}Numeric types:{0}"
      Dim msg4 As String
 = "{0}The following types are not CLS-compliant:{0}"
      
      ' Non-numeric types.
      Dim booleanVal As [Boolean]
      Dim charVal As [Char]
      Dim datetimeVal As DateTime
      
      ' Numeric types.
      Dim byteVal As [Byte]
      Dim int16Val As Int16
      Dim int32Val As Int32
      Dim int64Val As Int64
      Dim decimalVal As [Decimal]
      Dim singleVal As [Single]
      Dim doubleVal As [Double]
      
      ' The following types are not CLS-compliant.
      Dim sbyteVal As SByte
      Dim uint16Val As UInt16
      Dim uint32Val As UInt32
      Dim uint64Val As UInt64
      '
      Console.WriteLine(msg1, nl)
      
      ' Non-numeric types:
      Console.WriteLine(msg2, nl)
      ' DateTime
      ' TryParse:
      ' Assume current culture is en-US, and dates of the form: MMDDYYYY.
      result = DateTime.TryParse("7/4/2004 12:34:56",
 datetimeVal)
      Show(result, "DateTime #1", datetimeVal.ToString())
      
      ' Use fr-FR culture, and dates of the form: DDMMYYYY.
      ci = New CultureInfo("fr-FR")
      result = DateTime.TryParse("4/7/2004 12:34:56",
 ci, DateTimeStyles.None, datetimeVal)
      Show(result, "DateTime #2", datetimeVal.ToString())
      
      ' TryParseExact:
      ' Use fr-FR culture. The format, "G", is short date
 and long time.
      result = DateTime.TryParseExact("04/07/2004 12:34:56",
 "G", ci, DateTimeStyles.None, datetimeVal)
      Show(result, "DateTime #3", datetimeVal.ToString())
      
      ' Assume en-US culture.
      Dim dateFormats As String()
 =  {"f", "F", "g",
 "G"}
      result = DateTime.TryParseExact("7/4/2004 12:34:56 PM",
 dateFormats, Nothing, DateTimeStyles.None, datetimeVal)
      Show(result, "DateTime #4", datetimeVal.ToString())
      
      Console.WriteLine()
      ' Boolean
      result = [Boolean].TryParse("true", booleanVal)
      Show(result, "Boolean", booleanVal.ToString())
      ' Char
      result = [Char].TryParse("A", charVal)
      Show(result, "Char", charVal.ToString())
      
      ' Numeric types:
      Console.WriteLine(msg3, nl)
      ' Byte
      result = [Byte].TryParse("1", NumberStyles.Integer,
 Nothing, byteVal)
      Show(result, "Byte", byteVal.ToString())
      ' Int16
      result = Int16.TryParse("-2", NumberStyles.Integer,
 Nothing, int16Val)
      Show(result, "Int16", int16Val.ToString())
      ' Int32
      result = Int32.TryParse("3", NumberStyles.Integer,
 Nothing, int32Val)
      Show(result, "Int32", int32Val.ToString())
      ' Int64
      result = Int64.TryParse("4", NumberStyles.Integer,
 Nothing, int64Val)
      Show(result, "Int64", int64Val.ToString())
      ' Decimal
      result = [Decimal].TryParse("-5.5", NumberStyles.Number,
 Nothing, decimalVal)
      Show(result, "Decimal", decimalVal.ToString())
      ' Single
      result = [Single].TryParse("6.6", NumberStyles.Float
 Or NumberStyles.AllowThousands, Nothing,
 singleVal)
      Show(result, "Single", singleVal.ToString())
      ' Double
      result = [Double].TryParse("-7", NumberStyles.Float
 Or NumberStyles.AllowThousands, Nothing,
 doubleVal)
      Show(result, "Double", doubleVal.ToString())
      
      ' Use the simple Double.TryParse overload, but specify an invalid
 value.
      result = [Double].TryParse("abc", doubleVal)
      Show(result, "Double #2", doubleVal.ToString())
      '
      Console.WriteLine(msg4, nl)
      ' SByte
      result = SByte.TryParse("-8", NumberStyles.Integer,
 Nothing, sbyteVal)
      Show(result, "SByte", sbyteVal.ToString())
      ' UInt16
      result = UInt16.TryParse("9", NumberStyles.Integer,
 Nothing, uint16Val)
      Show(result, "UInt16", uint16Val.ToString())
      ' UInt32
      result = UInt32.TryParse("10", NumberStyles.Integer,
 Nothing, uint32Val)
      Show(result, "UInt32", uint32Val.ToString())
      ' UInt64
      result = UInt64.TryParse("11", NumberStyles.Integer,
 Nothing, uint64Val)
      Show(result, "UInt64", uint64Val.ToString())
   End Sub 'Main
   
   
   Protected Shared Sub
 Show(parseResult As Boolean, typeName As
 String, parseValue As String)
      Dim msgSuccess As String
 = "Parse for {0} = {1}"
      Dim msgFailure As String
 = "** Parse for {0} failed. Invalid input."
      '
      If parseResult = True Then
         Console.WriteLine(msgSuccess, typeName, parseValue)
      Else
         Console.WriteLine(msgFailure, typeName)
      End If
   End Sub 'Show
End Class 'Sample '
'This example produces the following results:
'
'This example demonstrates overloads of the TryParse method for
'several base types, as well as the TryParseExact method for DateTime.
'
'Non-numeric types:
'
'Parse for DateTime #1 = 7/4/2004 12:34:56 PM
'Parse for DateTime #2 = 7/4/2004 12:34:56 PM
'Parse for DateTime #3 = 7/4/2004 12:34:56 PM
'Parse for DateTime #4 = 7/4/2004 12:34:56 PM
'
'Parse for Boolean = True
'Parse for Char = A
'
'Numeric types:
'
'Parse for Byte = 1
'Parse for Int16 = -2
'Parse for Int32 = 3
'Parse for Int64 = 4
'Parse for Decimal = -5.5
'Parse for Single = 6.6
'Parse for Double = -7
'** Parse for Double #2 failed. Invalid input.
'
'The following types are not CLS-compliant:
'
'Parse for SByte = -8
'Parse for UInt16 = 9
'Parse for UInt32 = 10
'Parse for UInt64 = 11
'
// This example demonstrates overloads of the TryParse method for
// several base types, and the TryParseExact method for DateTime.

// In most cases, this example uses the most complex overload; that
 is, the overload 
// with the most parameters for a particular type. If a complex overload
 specifies 
// null (Nothing in Visual Basic) for the IFormatProvider parameter,
 formatting 
// information is obtained from the culture associated with the current
 thread. 
// If a complex overload specifies the style parameter, the parameter
 value is 
// the default value used by the equivalent simple overload.

using System;
using System.Globalization;

class Sample 
{
    public static void Main()
 
    {
    bool     result;
    CultureInfo ci;
    string   nl = Environment.NewLine;
    string   msg1 = 
             "This example demonstrates overloads of the TryParse method for{0}"
 +
             "several base types, as well as the TryParseExact
 method for DateTime.{0}";
    string   msg2 = "Non-numeric types:{0}";
    string   msg3 = "{0}Numeric types:{0}";
    string   msg4 = "{0}The following types are not CLS-compliant:{0}";

// Non-numeric types.
    Boolean  booleanVal;
    Char     charVal;
    DateTime datetimeVal;

// Numeric types.
    Byte     byteVal;
    Int16    int16Val;
    Int32    int32Val;
    Int64    int64Val;
    Decimal  decimalVal;
    Single   singleVal;
    Double   doubleVal;

// The following types are not CLS-compliant.
    SByte    sbyteVal;
    UInt16   uint16Val;
    UInt32   uint32Val;
    UInt64   uint64Val;
//
    Console.WriteLine(msg1, nl);

// Non-numeric types:
    Console.WriteLine(msg2, nl);
// DateTime
  // TryParse:
    // Assume current culture is en-US, and dates of the form: MMDDYYYY.
    result = DateTime.TryParse("7/4/2004 12:34:56", out datetimeVal);
    Show(result, "DateTime #1", datetimeVal.ToString());

    // Use fr-FR culture, and dates of the form: DDMMYYYY.
    ci = new CultureInfo("fr-FR");
    result = DateTime.TryParse("4/7/2004 12:34:56", 
             ci, DateTimeStyles.None, out datetimeVal);
    Show(result, "DateTime #2", datetimeVal.ToString());

  // TryParseExact:
    // Use fr-FR culture. The format, "G", is short date and
 long time.
    result = DateTime.TryParseExact("04/07/2004 12:34:56", "G",
 
             ci, DateTimeStyles.None, out datetimeVal);
    Show(result, "DateTime #3", datetimeVal.ToString());

    // Assume en-US culture.
    string[] dateFormats = {"f", "F", "g",
 "G"};
    result = DateTime.TryParseExact("7/4/2004 12:34:56 PM", 
             dateFormats, null, DateTimeStyles.None, 
             out datetimeVal);
    Show(result, "DateTime #4", datetimeVal.ToString());

    Console.WriteLine();
// Boolean
    result = Boolean.TryParse("true", out booleanVal);
    Show(result, "Boolean", booleanVal.ToString());
// Char
    result = Char.TryParse("A", out charVal);
    Show(result, "Char", charVal.ToString());

// Numeric types:
    Console.WriteLine(msg3, nl);
// Byte
    result = Byte.TryParse("1", NumberStyles.Integer, null,
 out byteVal);
    Show(result, "Byte", byteVal.ToString());
// Int16
    result = Int16.TryParse("-2", NumberStyles.Integer, null,
 out int16Val);
    Show(result, "Int16", int16Val.ToString());
// Int32
    result = Int32.TryParse("3", NumberStyles.Integer, null,
 out int32Val);
    Show(result, "Int32", int32Val.ToString());
// Int64
    result = Int64.TryParse("4", NumberStyles.Integer, null,
 out int64Val);
    Show(result, "Int64", int64Val.ToString());
// Decimal
    result = Decimal.TryParse("-5.5", NumberStyles.Number, null,
 out decimalVal);
    Show(result, "Decimal", decimalVal.ToString());
// Single
    result = Single.TryParse("6.6", 
             (NumberStyles.Float | NumberStyles.AllowThousands), 
             null, out singleVal);
    Show(result, "Single", singleVal.ToString());
// Double
    result = Double.TryParse("-7", 
             (NumberStyles.Float | NumberStyles.AllowThousands), 
             null, out doubleVal);
    Show(result, "Double", doubleVal.ToString());

// Use the simple Double.TryParse overload, but specify an invalid value.

    result = Double.TryParse("abc", out doubleVal);
    Show(result, "Double #2", doubleVal.ToString());
//
    Console.WriteLine(msg4, nl);
// SByte
    result = SByte.TryParse("-8", NumberStyles.Integer, null,
 out sbyteVal);
    Show(result, "SByte", sbyteVal.ToString());
// UInt16
    result = UInt16.TryParse("9", NumberStyles.Integer, null,
 out uint16Val);
    Show(result, "UInt16", uint16Val.ToString());
// UInt32
    result = UInt32.TryParse("10", NumberStyles.Integer, null,
 out uint32Val);
    Show(result, "UInt32", uint32Val.ToString());
// UInt64
    result = UInt64.TryParse("11", NumberStyles.Integer, null,
 out uint64Val);
    Show(result, "UInt64", uint64Val.ToString());
    }

    protected static void
 Show(bool parseResult, string typeName, 
                               string parseValue)
    {
    string msgSuccess = "Parse for {0}
 = {1}";
    string msgFailure = "** Parse for
 {0} failed. Invalid input.";
//
    if (parseResult == true)
        Console.WriteLine(msgSuccess, typeName, parseValue);
    else
        Console.WriteLine(msgFailure, typeName);
   }
}
/*
This example produces the following results:

This example demonstrates overloads of the TryParse method for
several base types, as well as the TryParseExact method for
 DateTime.

Non-numeric types:

Parse for DateTime #1 = 7/4/2004 12:34:56 PM
Parse for DateTime #2 = 7/4/2004 12:34:56 PM
Parse for DateTime #3 = 7/4/2004 12:34:56 PM
Parse for DateTime #4 = 7/4/2004 12:34:56 PM

Parse for Boolean = True
Parse for Char = A

Numeric types:

Parse for Byte = 1
Parse for Int16 = -2
Parse for Int32 = 3
Parse for Int64 = 4
Parse for Decimal = -5.5
Parse for Single = 6.6
Parse for Double = -7
** Parse for Double #2 failed. Invalid input.

The following types are not CLS-compliant:

Parse for SByte = -8
Parse for UInt16 = 9
Parse for UInt32 = 10
Parse for UInt64 = 11
*/
// This example demonstrates overloads of the TryParse method for
// several base types, and the TryParseExact method for DateTime.
// In most cases, this example uses the most complex overload; that
 is, the overload
// with the most parameters for a particular type. If a complex overload
 specifies
// null (Nothing in Visual Basic) for the IFormatProvider parameter,
 formatting
// information is obtained from the culture associated with the current
 thread.
// If a complex overload specifies the style parameter, the parameter
 value is
// the default value used by the equivalent simple overload.
using namespace System;
using namespace System::Globalization;

static void Show( bool parseResult,
 String^ typeName, String^ parseValue )
{
   String^ msgSuccess = L"Parse for {0} = {1}";
   String^ msgFailure = L"** Parse for {0} failed. Invalid
 input.";
   
   //
   if ( parseResult == true )
      Console::WriteLine( msgSuccess, typeName, parseValue );
   else
      Console::WriteLine( msgFailure, typeName );
}

void main()
{
   bool result;
   CultureInfo^ ci;
   String^ nl = Environment::NewLine;
   String^ msg1 = L"This example demonstrates overloads of the TryParse method
 for{0}"
   L"several base types, as well as the TryParseExact method for
 DateTime.{0}";
   String^ msg2 = L"Non-numeric types:{0}";
   String^ msg3 = L"{0}Numeric types:{0}";
   String^ msg4 = L"{0}The following types are not CLS-compliant:{0}";
   
   // Non-numeric types.
   Boolean booleanVal;
   Char charVal;
   DateTime datetimeVal;
   
   // Numeric types.
   Byte byteVal;
   Int16 int16Val;
   Int32 int32Val;
   Int64 int64Val;
   Decimal decimalVal;
   Single singleVal;
   Double doubleVal;
   
   // The following types are not CLS-compliant.
   SByte sbyteVal;
   UInt16 uint16Val;
   UInt32 uint32Val;
   UInt64 uint64Val;
   
   //
   Console::WriteLine( msg1, nl );
   
   // Non-numeric types:
   Console::WriteLine( msg2, nl );
   
   // DateTime
   // TryParse:
   // Assume current culture is en-US, and dates of the form: MMDDYYYY.
   result = DateTime::TryParse( L"7/4/2004 12:34:56",  datetimeVal );
   Show( result, L"DateTime #1", datetimeVal.ToString() );
   
   // Use fr-FR culture, and dates of the form: DDMMYYYY.
   ci = gcnew CultureInfo( L"fr-FR" );
   result = DateTime::TryParse( L"4/7/2004 12:34:56", ci, DateTimeStyles::None,
  datetimeVal );
   Show( result, L"DateTime #2", datetimeVal.ToString() );
   
   // TryParseExact:
   // Use fr-FR culture. The format, "G", is short date and
 long time.
   result = DateTime::TryParseExact( L"04/07/2004 12:34:56", L"G",
 ci, DateTimeStyles::None,  datetimeVal );
   Show( result, L"DateTime #3", datetimeVal.ToString() );
   
   // Assume en-US culture.
   array<String^>^dateFormats = {L"f",L"F",L"g"
,L"G"};
   result = DateTime::TryParseExact( L"7/4/2004 12:34:56 PM", dateFormats,
 nullptr, DateTimeStyles::None,  datetimeVal );
   Show( result, L"DateTime #4", datetimeVal.ToString() );
   Console::WriteLine();
   
   // Boolean
   result = Boolean::TryParse( L"true",  booleanVal
 );
   Show( result, L"Boolean", booleanVal.ToString() );
   
   // Char
   result = Char::TryParse( L"A",  charVal );
   Show( result, L"Char", charVal.ToString() );
   
   // Numeric types:
   Console::WriteLine( msg3, nl );
   
   // Byte
   result = Byte::TryParse( L"1", NumberStyles::Integer, nullptr,  byteVal
 );
   Show( result, L"Byte", byteVal.ToString() );
   
   // Int16
   result = Int16::TryParse( L"-2", NumberStyles::Integer, nullptr,  int16Val
 );
   Show( result, L"Int16", int16Val.ToString() );
   
   // Int32
   result = Int32::TryParse( L"3", NumberStyles::Integer, nullptr,  int32Val
 );
   Show( result, L"Int32", int32Val.ToString() );
   
   // Int64
   result = Int64::TryParse( L"4", NumberStyles::Integer, nullptr,  int64Val
 );
   Show( result, L"Int64", int64Val.ToString() );
   
   // Decimal
   result = Decimal::TryParse( L"-5.5", NumberStyles::Number, nullptr,
  decimalVal );
   Show( result, L"Decimal", decimalVal.ToString() );
   
   // Single
   result = Single::TryParse( L"6.6", static_cast<NumberStyles>((NumberStyles::Float
 | NumberStyles::AllowThousands)), nullptr,  singleVal );
   Show( result, L"Single", singleVal.ToString() );
   
   // Double
   result = Double::TryParse( L"-7", static_cast<NumberStyles>(NumberStyles::Float
 | NumberStyles::AllowThousands), nullptr,  doubleVal );
   Show( result, L"Double", doubleVal.ToString() );
   
   // Use the simple Double.TryParse overload, but specify an invalid
 value.
   result = Double::TryParse( L"abc",  doubleVal );
   Show( result, L"Double #2", doubleVal.ToString() );
   
   //
   Console::WriteLine( msg4, nl );
   
   // SByte
   result = SByte::TryParse( L"-8", NumberStyles::Integer, nullptr,  sbyteVal
 );
   Show( result, L"SByte", sbyteVal.ToString() );
   
   // UInt16
   result = UInt16::TryParse( L"9", NumberStyles::Integer, nullptr,  uint16Val
 );
   Show( result, L"UInt16", uint16Val.ToString() );
   
   // UInt32
   result = UInt32::TryParse( L"10", NumberStyles::Integer, nullptr,  uint32Val
 );
   Show( result, L"UInt32", uint32Val.ToString() );
   
   // UInt64
   result = UInt64::TryParse( L"11", NumberStyles::Integer, nullptr,  uint64Val
 );
   Show( result, L"UInt64", uint64Val.ToString() );
}

/*
This example produces the following results:

This example demonstrates overloads of the TryParse method for
several base types, as well as the TryParseExact method for
 DateTime.

Non-numeric types:

Parse for DateTime #1 = 7/4/2004 12:34:56 PM
Parse for DateTime #2 = 7/4/2004 12:34:56 PM
Parse for DateTime #3 = 7/4/2004 12:34:56 PM
Parse for DateTime #4 = 7/4/2004 12:34:56 PM

Parse for Boolean = True
Parse for Char = A

Numeric types:

Parse for Byte = 1
Parse for Int16 = -2
Parse for Int32 = 3
Parse for Int64 = 4
Parse for Decimal = -5.5
Parse for Single = 6.6
Parse for Double = -7
** Parse for Double #2 failed. Invalid input.

The following types are not CLS-compliant:

Parse for SByte = -8
Parse for UInt16 = 9
Parse for UInt32 = 10
Parse for UInt64 = 11
*/
// This example demonstrates overloads of the TryParse method for
// several base types, and the TryParseExact method for DateTime.
// In most cases, this example uses the most complex overload; that
 is, 
// the overload with the most parameters for a particular type. If a
 
// complex overload specifies null (Nothing in Visual Basic) for the
 
// IFormatProvider parameter, formatting information is obtained from
 
// the culture associated with the current thread. If a complex overload
 
// specifies the style parameter, the parameter value is the default
 value 
// used by the equivalent simple overload.

import System.*;
import System.Globalization.*;

class Sample
{
    public static void main(String[]
 args)
    {
        boolean result;
        CultureInfo ci = null;
        String nl = Environment.get_NewLine();
        String msg1 = "This example demonstrates overloads of the TryParse "
 
            + "method for{0}several base
 types, as well as the TryParseExact " 
            + "method for DateTime.{0}";
        String msg2 = "Non-numeric types:{0}";
        String msg3 = "{0}Numeric types:{0}";
        String msg4 = "{0}The following types are not CLS-compliant:{0}";
        // Non-numeric types.
        System.Boolean booleanVal = (System.Boolean)false;
        System.Char charVal = (System.Char)0;
        DateTime dateTimeVal = System.DateTime.get_Now();
        // Numeric types.
        Byte byteVal = null;
        Int16 int16Val = (Int16)0;
        Int32 int32Val = (Int32)0;
        Int64 int64Val = (Int64)0;
        Decimal decimalVal = new Decimal(0);
        Single singleVal = (Single)0;
        //Double doubleVal = new Double(0);
        double doubleVal = 0;
        // The following types are not CLS-compliant.
        SByte sByteVal = (System.SByte)0;
        UInt16 uInt16Val = (UInt16)0;
        UInt32 uInt32Val = (UInt32)0;
        UInt64 uInt64Val = (UInt64)0;
        //
        Console.WriteLine(msg1, nl);
        // Non-numeric types:
        Console.WriteLine(msg2, nl);
        // DateTime
        // TryParse:
        // Assume current culture is en-US, and dates of the form: MMDDYYYY.
        result = DateTime.TryParse("7/4/2004 12:34:56", dateTimeVal);
        Show(result, "DateTime #1", dateTimeVal.ToString());
        // Use fr-FR culture, and dates of the form: DDMMYYYY.
        ci = new CultureInfo("fr-FR");
        result = DateTime.TryParse("4/7/2004 12:34:56", ci,
            DateTimeStyles.None, dateTimeVal);
        Show(result, "DateTime #2", dateTimeVal.ToString());
        // TryParseExact:
        // Use fr-FR culture. The format, "G", is short date and
 long time.
        result = DateTime.TryParseExact("04/07/2004 12:34:56", "G",
 ci, 
            DateTimeStyles.None, dateTimeVal);
        Show(result, "DateTime #3", dateTimeVal.ToString());
        // Assume en-US culture.
        String dateFormats[] =  { "f", "F", "g", "G"
 };
        result = DateTime.TryParseExact("7/4/2004 12:34:56 PM", dateFormats,
 
            null, DateTimeStyles.None, dateTimeVal);
        Show(result, "DateTime #4", dateTimeVal.ToString());

        Console.WriteLine();
        // Boolean
        boolean tempBooleanVal = System.Convert.ToBoolean(booleanVal);
        result = System.Boolean.TryParse("true",/**@out*/
 tempBooleanVal);
        Show(result, "Boolean", System.Convert.ToString(tempBooleanVal));
        // Char
        char tempCharVal = System.Convert.ToChar(charVal);
        result = Char.TryParse("A", /**@out*/tempCharVal);
        Show(result, "Char", System.Convert.ToString(tempCharVal));
        // Numeric types:
        Console.WriteLine(msg3, nl);
        // Byte
        ubyte tempByteVal = System.Convert.ToByte(byteVal);
        result = System.Byte.TryParse("1", NumberStyles.Integer, null
,
            /**@out*/tempByteVal);
        Show(result, "Byte", System.Convert.ToString(tempByteVal));
        // Int16
        short tempInt16Val = System.Convert.ToInt16(int16Val);
        result = Int16.TryParse("-2", NumberStyles.Integer, null
,
            /**@out*/tempInt16Val);
        Show(result, "Int16", System.Convert.ToString(tempInt16Val));
        // Int32
        int tempInt32Val = System.Convert.ToInt32(int32Val);
        result = Int32.TryParse("3", NumberStyles.Integer, null
,
            /**@out*/tempInt32Val);
        Show(result, "Int32", System.Convert.ToString(tempInt32Val));
        // Int64
        long tempInt64Val = System.Convert.ToInt64(int64Val);
        result = Int64.TryParse("4", NumberStyles.Integer, null
,
            /**@out*/tempInt64Val);
        Show(result, "Int64", System.Convert.ToString(tempInt64Val));
        // Decimal
        result = Decimal.TryParse("-5.5", NumberStyles.Number, null
,
            /**@out*/decimalVal);
        Show(result, "Decimal", decimalVal.ToString());
        // Single
        float tempSingleVal = System.Convert.ToSingle(singleVal);
        result = Single.TryParse("6.6", NumberStyles.Float 
            | NumberStyles.AllowThousands, null, /**@out*/tempSingleVal);
        Show(result, "Single", System.Convert.ToString(tempSingleVal));
        // Double
        result = System.Double.TryParse("-7", NumberStyles.Float 
            | NumberStyles.AllowThousands, null, /**@out*/doubleVal);
        Show(result, "Double", System.Convert.ToString(doubleVal));
        // Use the simple Double.TryParse overload, but specify an invalid
 value.
        result = System.Double.TryParse("abc", doubleVal);
        Show(result, "Double #2", System.Convert.ToString(doubleVal));
        //
        Console.WriteLine(msg4, nl);
        // SByte
        byte tempSByteVal = System.Convert.ToSByte(sByteVal);
        result = SByte.TryParse("-8", NumberStyles.Integer, null
,
            /**@out*/tempSByteVal);
        Show(result, "SByte", System.Convert.ToString(tempSByteVal));
        // UInt16
        result = UInt16.TryParse("9", NumberStyles.Integer, null
,
            /**@out*/uInt16Val);
        Show(result, "UInt16", uInt16Val.ToString());
        // UInt32
        result = UInt32.TryParse("10", NumberStyles.Integer, null
,
            /**@out*/uInt32Val);
        Show(result, "UInt32", uInt32Val.ToString());
        // UInt64
        result = UInt64.TryParse("11", NumberStyles.Integer, null
,
            /**@out*/uInt64Val);
        Show(result, "UInt64", uInt64Val.ToString());
    } //main

    protected static void
 Show(boolean parseResult,
        String typeName, String parseValue)
    {
        String msgSuccess = "Parse for {0} = {1}";
        String msgFailure = "** Parse for {0} failed. Invalid
 input.";
        //
        if (parseResult == true) {
            Console.WriteLine(msgSuccess, typeName, parseValue);
        }
        else {
            Console.WriteLine(msgFailure, typeName);
        }
    } //Show
} //Sample 
 /*
This example produces the following results:

This example demonstrates overloads of the TryParse method for
several base types, as well as the TryParseExact method for
 DateTime.

Non-numeric types:

Parse for DateTime #1 = 7/4/2004 12:34:56 PM
Parse for DateTime #2 = 7/4/2004 12:34:56 PM
Parse for DateTime #3 = 7/4/2004 12:34:56 PM
Parse for DateTime #4 = 7/4/2004 12:34:56 PM

Parse for Boolean = True
Parse for Char = A

Numeric types:

Parse for Byte = 1
Parse for Int16 = -2
Parse for Int32 = 3
Parse for Int64 = 4
Parse for Decimal = -5.5
Parse for Single = 6.6
Parse for Double = -7
** Parse for Double #2 failed. Invalid input.

The following types are not CLS-compliant:

Parse for SByte = -8
Parse for UInt16 = 9
Parse for UInt32 = 10
Parse for UInt64 = 11
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からBoolean.TryParse メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からBoolean.TryParse メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からBoolean.TryParse メソッドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Boolean.TryParse メソッド」の関連用語

Boolean.TryParse メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Boolean.TryParse メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS