asp.net.ph

DateTime.CompareTo Method

System Namespace   DateTime Structure


Compares this instance to a specified object and returns an indication of their relative values.

[ VB ]
<Serializable>
NotOverridable Public Function CompareTo ( _
   ByVal value As Object _
} As Integer

[ C# ]
[ Serializable ]
public int CompareTo (
   object value
};

[ C++ ]
[ Serializable ]
public: __sealed int CompareTo (
   Object* value
};

[ JScript ]
public Serializable
function CompareTo (
   value : Object
} : int;

Parameters

value
An object to compare, or a null reference ( Nothing in Visual Basic ).

Return Value

A signed number indicating the relative values of this instance and value.

Value Description
Less than zero This instance is less than value.
Zero This instance is equal to value.
Greater than zero This instance is greater than value, or value is a null reference ( Nothing in Visual Basic ).

Exceptions


Exception Type Condition
ArgumentException value is not a DateTime.

Remarks

Any instance of DateTime, regardless of its value, is considered greater than a null reference ( Nothing in Visual Basic ).

value must be an instance of DateTime or a null reference ( Nothing ); otherwise, an exception is thrown.

Example

The below code snippet demonstrates using the CompareTo method.

System.DateTime theDay =
        new System.DateTime ( System.DateTime.Today.Year, 7, 28 );

int compareValue;
try {
   compareValue = theDay.CompareTo ( System.DateTime.Today );
}
catch ( ArgumentException ) {
   System.Response.WriteLine ( "Value is not a DateTime" );
   return;
}

if ( compareValue < 0 ) {
   System.Response.WriteLine ( "{ 0:d } is in the past.", theDay );
}
else if ( compareValue == 0 ) {
   System.Response.WriteLine ( "{ 0:d } is today!", theDay );
}
else if ( compareValue == 1 ) {
   System.Response.WriteLine ( "Value is null" );
}
// compareValue > 0 && compareValue != 1
else {
   System.Response.WriteLine ( "{ 0:d } has not come yet.", theDay );
}
  C# VB

See Also

DateTime Members   Compare   Equals Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph