CompareTo Method (Object)
Collapse the table of content
Expand the table of content

Char.CompareTo Method (Object)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Compares this instance to a specified object and returns an integer that indicates whether this instance precedes, follows, or has the same position in the sort order as the specified object.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function CompareTo ( _
	value As Object _
) As Integer

Parameters

value
Type: System.Object
An object to compare this instance to, or Nothing.

Return Value

Type: System.Int32
A signed number that indicates the position of this instance in the sort order in relation to the value parameter.

Return Value

Description

Less than zero

This instance precedes value.

Zero

This instance has the same position in the sort order as value.

Greater than zero

This instance is follows value.

-or-

value is Nothing.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
ArgumentException

value is not a Char object.

The CompareTo method implements the IComparable interface.

The value parameter must be Nothing or an instance of Char; otherwise, an exception is thrown.

The comparison performed by this method is based on the encoded values of this instance and value, not necessarily their lexicographical characteristics. Any instance of Char, regardless of its value, is considered greater than Nothing.

The following example demonstrates CompareTo.



Module Example

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim chA As Char
      chA = "A"c
      Dim chB As Char
      chB = "B"c

      outputBlock.Text &= chA.CompareTo("A"c)   ' Output: "0" (meaning they're equal & vbCrLf
      outputBlock.Text &= "b"c.CompareTo(chB)   ' Output: "32" (meaning 'b' is 32 greater than 'B' & vbCrLf
      outputBlock.Text &= chA.CompareTo(chB)    ' Output: "-1" (meaning 'A' is less than 'B' by 1 & vbCrLf

   End Sub

End Module


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft