Implement IComparable
Purpose
Using this Code Provider, you can easily add the implementation of the IComparable interface to any class containing at least one field or property. This Code Provider adds the IComparable and IComparable<T> implementations along with the CompareTo methods to the class. The CompareTo method bodies are generated automatically.
Availability
Available when the caret is on a class declaration.
Usage
Place the caret on a class declaration. The class must contain at least one field or property.
Note
The blinking cursor shows the caret's position at which the Code Provider is available.
public│ class Temperature {
protected double temperatureF;
public double Fahrenheit {
get {
return this.temperatureF;
}
set {
this.temperatureF = value;
}
}
public double Celsius {
get {
return (this.temperatureF - 32) * (5.0 / 9);
}
set {
this.temperatureF = (value * 9.0 / 5) + 32;
}
}
}
Public│ Class Temperature
Protected temperatureF As Double
Public Property Fahrenheit() As Double
Get
Return Me.temperatureF
End Get
Set(ByVal value As Double)
Me.temperatureF = value
End Set
End Property
Public Property Celsius() As Double
Get
Return (Me.temperatureF - 32) * (5.0 \ 9)
End Get
Set(ByVal value As Double)
Me.temperatureF = (value * 9.0 / 5) + 32
End Set
End Property
End Class
- Use the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions Menu.
- Select Implement IComparable from the menu.
After execution, the Code Provider adds the implementation of the IComparable interface and type-specific IComparable<T> interface. It also adds the corresponding CompareTo methods with their common implementation.
public class Temperature : IComparable, IComparable<Temperature> {
protected double temperatureF;
public double Fahrenheit {
get {
return this.temperatureF;
}
set {
this.temperatureF = value;
}
}
public double Celsius {
get {
return (this.temperatureF - 32) * (5.0 / 9);
}
set {
this.temperatureF = (value * 9.0 / 5) + 32;
}
}
public int CompareTo(object obj) {
if (obj == null)
return 1;
Temperature other = obj as Temperature;
if (other == null)
throw new ArgumentException("obj is not a Temperature");
return CompareTo(other);
}
public int CompareTo(Temperature other) {
if (other == null)
return 1;
int result = 0;
result = this.temperatureF.CompareTo(other.temperatureF);
if (result != 0)
return result;
return result;
}
}
Public Class Temperature
Implements IComparable, IComparable(Of Temperature)
Protected temperatureF As Double
Public Property Fahrenheit() As Double
Get
Return Me.temperatureF
End Get
Set(ByVal value As Double)
Me.temperatureF = value
End Set
End Property
Public Property Celsius() As Double
Get
Return (Me.temperatureF - 32) * (5.0 \ 9)
End Get
Set(ByVal value As Double)
Me.temperatureF = (value * 9.0 / 5) + 32
End Set
End Property
Public Function CompareTo(obj As Object) As Integer _
Implements IComparable.CompareTo
If obj Is Nothing Then
Return 1
End If
Dim other As Temperature = IIf(TypeOf obj Is Temperature,
CType(obj, Temperature), Nothing)
If other Is Nothing Then
Throw New ArgumentException("obj is not a Temperature")
End If
Return CompareTo(other)
End Function
Public Function CompareTo(other As Temperature) As Integer _
Implements IComparable(Of Temperature).CompareTo
If other Is Nothing Then
Return 1
End If
Dim result As Integer = 0
result = Me.temperatureF.CompareTo(other.temperatureF)
If result <> 0 Then
Return result
End If
Return result
End Function
End Class
See Also
We are updating the DevExpress product documentation website and this page is part of our new experience. During this transition period, product documentation remains available in our previous format at documentation.devexpress.com. Learn More...