指定したオブジェクトとこのインスタンスを比較し、これらの相対値を示す値を返します。
Public Overridable Function CompareTo( _
ByVal value As Object _) As Integer
[C#]
public virtual int CompareTo(objectvalue);
[C++]
public: virtual int CompareTo(Object* value);
[JScript]
public function CompareTo(
value : Object) : int;
パラメータ
- value
比較対象のオブジェクト、または null 参照 (Visual Basic では Nothing) 。
戻り値
値 | 条件 |
---|---|
-1 | このインスタンスの値が value の値未満。 |
0 | このインスタンスの値が value の値に等しい。 |
1 | このインスタンスの値が value の値より大きい。
または value が null 参照 (Visual Basic では Nothing) です。 |
例外
例外の種類 | 条件 |
---|---|
ArgumentException | value が TimeSpan ではありません。 |
解説
TimeSpan のすべてのインスタンスは、その値に関係なく、 null 参照 (Visual Basic では Nothing) より大きいと見なされます。
value パラメータが TimeSpan のインスタンスか null 参照 (Nothing) であることが必要です。それ以外の場合は例外がスローされます。
使用例
[Visual Basic, C#, C++] 次に示すのは、 CompareTo メソッドを使用して、 TimeSpan およびその他の複数のオブジェクトを TimeSpan の参照値と比較するコード例です。
' Example of the TimeSpan.CompareTo( Object ) and
' TimeSpan.Equals( Object ) methods.
Imports System
Imports Microsoft.VisualBasic
Module TSCompToEqualsObjDemo
' Compare the TimeSpan to the Object parameters,
' and display the Object parameters with the results.
Sub CompTimeSpanToObject( Left as TimeSpan, Right as Object, _
RightText as String )
Console.WriteLine( "{0,-33}{1}", "Object: " & RightText, _
Right )
Console.WriteLine( "{0,-33}{1}", "Left.Equals( Object )", _
Left.Equals( Right ) )
Console.Write( "{0,-33}", "Left.CompareTo( Object )" )
' Catch the exception if CompareTo( ) throws one.
Try
Console.WriteLine( "{0}" & vbCrLf, _
Left.CompareTo( Right ) )
Catch ex As Exception
Console.WriteLine( "Error: {0}" & vbCrLf, ex.Message )
End Try
End Sub
Sub Main( )
Dim Left as new TimeSpan( 0, 5, 0 )
Console.WriteLine( _
"This example of the TimeSpan.Equals( Object ) " & _
"and " & vbCrLf & "TimeSpan.CompareTo( Object ) " & _
"methods generates the " & vbCrLf & _
"following output by creating several " & _
"different TimeSpan " & vbCrLf & "objects and " & _
"comparing them with a 5-minute TimeSpan." & vbCrLf )
Console.WriteLine( "{0,-33}{1}" & vbCrLf, _
"Left: TimeSpan( 0, 5, 0 )", Left )
' Create objects to compare with a 5-minute TimeSpan.
CompTimeSpanToObject( Left, new TimeSpan( 0, 0, 300 ), _
"TimeSpan( 0, 0, 300 )" )
CompTimeSpanToObject( Left, new TimeSpan( 0, 5, 1 ), _
"TimeSpan( 0, 5, 1 )" )
CompTimeSpanToObject( Left, new TimeSpan( 0, 5, -1 ), _
"TimeSpan( 0, 5, -1 )" )
CompTimeSpanToObject( Left, new TimeSpan( 3000000000 ), _
"TimeSpan( 3000000000 )" )
CompTimeSpanToObject( Left, 3000000000L, "Long 3000000000L" )
CompTimeSpanToObject( Left, "00:05:00", _
"String ""00:05:00""" )
End Sub
End Module
' This example of the TimeSpan.Equals( Object ) and
' TimeSpan.CompareTo( Object ) methods generates the
' following output by creating several different TimeSpan
' objects and comparing them with a 5-minute TimeSpan.
'
' Left: TimeSpan( 0, 5, 0 ) 00:05:00
'
' Object: TimeSpan( 0, 0, 300 ) 00:05:00
' Left.Equals( Object ) True
' Left.CompareTo( Object ) 0
'
' Object: TimeSpan( 0, 5, 1 ) 00:05:01
' Left.Equals( Object ) False
' Left.CompareTo( Object ) -1
'
' Object: TimeSpan( 0, 5, -1 ) 00:04:59
' Left.Equals( Object ) False
' Left.CompareTo( Object ) 1
'
' Object: TimeSpan( 3000000000 ) 00:05:00
' Left.Equals( Object ) True
' Left.CompareTo( Object ) 0
'
' Object: Long 3000000000L 3000000000
' Left.Equals( Object ) False
' Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
'
' Object: String "00:05:00" 00:05:00
' Left.Equals( Object ) False
' Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
[C#]
// Example of the TimeSpan.CompareTo( Object ) and
// TimeSpan.Equals( Object ) methods.
using System;
class TSCompToEqualsObjDemo
{
// Compare the TimeSpan to the Object parameters,
// and display the Object parameters with the results.
static void CompTimeSpanToObject( TimeSpan Left, object Right,
string RightText )
{
Console.WriteLine( "{0,-33}{1}", "Object: " + RightText,
Right );
Console.WriteLine( "{0,-33}{1}", "Left.Equals( Object )",
Left.Equals( Right ) );
Console.Write( "{0,-33}", "Left.CompareTo( Object )" );
// Catch the exception if CompareTo( ) throws one.
try
{
Console.WriteLine( "{0}\n", Left.CompareTo( Right ) );
}
catch( Exception ex )
{
Console.WriteLine( "Error: {0}\n", ex.Message );
}
}
static void Main( )
{
TimeSpan Left = new TimeSpan( 0, 5, 0 );
Console.WriteLine(
"This example of the TimeSpan.Equals( Object ) " +
"and \nTimeSpan.CompareTo( Object ) methods generates " +
"the \nfollowing output by creating several different " +
"TimeSpan \nobjects and comparing them with a " +
"5-minute TimeSpan.\n" );
Console.WriteLine( "{0,-33}{1}\n",
"Left: TimeSpan( 0, 5, 0 )", Left );
// Create objects to compare with a 5-minute TimeSpan.
CompTimeSpanToObject( Left, new TimeSpan( 0, 0, 300 ),
"TimeSpan( 0, 0, 300 )" );
CompTimeSpanToObject( Left, new TimeSpan( 0, 5, 1 ),
"TimeSpan( 0, 5, 1 )" );
CompTimeSpanToObject( Left, new TimeSpan( 0, 5, -1 ),
"TimeSpan( 0, 5, -1 )" );
CompTimeSpanToObject( Left, new TimeSpan( 3000000000 ),
"TimeSpan( 3000000000 )" );
CompTimeSpanToObject( Left, 3000000000L,
"long 3000000000L" );
CompTimeSpanToObject( Left, "00:05:00",
"string \"00:05:00\"" );
}
}
/*
This example of the TimeSpan.Equals( Object ) and
TimeSpan.CompareTo( Object ) methods generates the
following output by creating several different TimeSpan
objects and comparing them with a 5-minute TimeSpan.
Left: TimeSpan( 0, 5, 0 ) 00:05:00
Object: TimeSpan( 0, 0, 300 ) 00:05:00
Left.Equals( Object ) True
Left.CompareTo( Object ) 0
Object: TimeSpan( 0, 5, 1 ) 00:05:01
Left.Equals( Object ) False
Left.CompareTo( Object ) -1
Object: TimeSpan( 0, 5, -1 ) 00:04:59
Left.Equals( Object ) False
Left.CompareTo( Object ) 1
Object: TimeSpan( 3000000000 ) 00:05:00
Left.Equals( Object ) True
Left.CompareTo( Object ) 0
Object: long 3000000000L 3000000000
Left.Equals( Object ) False
Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
Object: string "00:05:00" 00:05:00
Left.Equals( Object ) False
Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
*/
[C++]
// Example of the TimeSpan::CompareTo( Object* ) and
// TimeSpan::Equals( Object* ) methods.
#using <mscorlib.dll>
using namespace System;
// Compare the TimeSpan to the Object parameters,
// and display the Object parameters with the results.
void CompTimeSpanToObject( TimeSpan Left, Object* Right,
String* RightText )
{
Console::WriteLine( S"{0,-33}{1}",
String::Concat( S"Object: ", RightText ), Right );
Console::WriteLine( S"{0,-33}{1}", S"Left.Equals( Object )",
__box( Left.Equals( Right ) ) );
Console::Write( S"{0,-33}", S"Left.CompareTo( Object )" );
// Catch the exception if CompareTo( ) throws one.
try
{
Console::WriteLine( S"{0}\n",
__box( Left.CompareTo( Right ) ) );
}
catch( Exception* ex )
{
Console::WriteLine( S"Error: {0}\n", ex->Message );
}
}
void main( )
{
TimeSpan Left = TimeSpan( 0, 5, 0 );
Console::WriteLine(
S"This example of the TimeSpan::Equals( Object* ) "
S"and \nTimeSpan::CompareTo( Object* ) methods generates "
S"the \nfollowing output by creating several different "
S"TimeSpan \nobjects and comparing them with a "
S"5-minute TimeSpan.\n" );
Console::WriteLine( S"{0,-33}{1}\n",
S"Left: TimeSpan( 0, 5, 0 )", __box( Left ) );
// Create objects to compare with a 5-minute TimeSpan.
CompTimeSpanToObject( Left, __box( TimeSpan( 0, 0, 300 ) ),
S"TimeSpan( 0, 0, 300 )" );
CompTimeSpanToObject( Left, __box( TimeSpan( 0, 5, 1 ) ),
S"TimeSpan( 0, 5, 1 )" );
CompTimeSpanToObject( Left, __box( TimeSpan( 0, 5, -1 ) ),
S"TimeSpan( 0, 5, -1 )" );
CompTimeSpanToObject( Left, __box( TimeSpan( 3000000000 ) ),
S"TimeSpan( 3000000000 )" );
CompTimeSpanToObject( Left, __box( 3000000000L ),
S"__int64 3000000000L" );
CompTimeSpanToObject( Left, S"00:05:00", S"String \"00:05:00\"" );
}
/*
This example of the TimeSpan::Equals( Object* ) and
TimeSpan::CompareTo( Object* ) methods generates the
following output by creating several different TimeSpan
objects and comparing them with a 5-minute TimeSpan.
Left: TimeSpan( 0, 5, 0 ) 00:05:00
Object: TimeSpan( 0, 0, 300 ) 00:05:00
Left.Equals( Object ) True
Left.CompareTo( Object ) 0
Object: TimeSpan( 0, 5, 1 ) 00:05:01
Left.Equals( Object ) False
Left.CompareTo( Object ) -1
Object: TimeSpan( 0, 5, -1 ) 00:04:59
Left.Equals( Object ) False
Left.CompareTo( Object ) 1
Object: TimeSpan( 3000000000 ) 00:05:00
Left.Equals( Object ) True
Left.CompareTo( Object ) 0
Object: __int64 3000000000L 3000000000
Left.Equals( Object ) False
Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
Object: String "00:05:00" 00:05:00
Left.Equals( Object ) False
Left.CompareTo( Object ) Error: Object must be of type TimeSpan.
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
TimeSpan 構造体 | TimeSpan メンバ | System 名前空間 | Object | Compare | Equals