This documentation is archived and is not being maintained.

UInt16.CompareTo Method

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

The UInt16 type is not CLS-compliant. The CLS-compliant alternative type is Int32. Int16 can be used when the original value is less than half of UInt16.MaxValue. For more information about CLS compliance, see What is the Common Language Specification.

[Visual Basic]
Public Overridable Function CompareTo( _
   ByVal value As Object _
) As Integer
[C#]
public virtual int CompareTo(
 object value
);
[C++]
public: virtual int CompareTo(
 Object* value
);
[JScript]
public 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.

Return 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 UInt16.

Remarks

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

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

Example

[C#, C++, JScript] The following sample demonstrates the CompareTo method.

[C#] 
public class Temperature : IComparable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public int CompareTo(object obj) {
        if(obj is Temperature) {
            Temperature temp = (Temperature) obj;

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    // The value holder
    protected ushort m_value;

    public ushort Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }
}

[C++] 
public __gc class Temperature : public IComparable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
// The value holder
protected:
    short m_value;

public:
    Int32 CompareTo(Object* obj) {
        if(obj->GetType() == __typeof(Temperature)) {
            Temperature* temp = dynamic_cast<Temperature*>(obj);
            return m_value.CompareTo(__box(temp->m_value));
        }

        throw new ArgumentException("object is not a Temperature");
    };

    __property short get_Value() {
        return m_value;
    };

    __property void set_Value( short value) {
        m_value = value;
    };

    __property short get_Celsius() {
        return (short)((m_value-32)/2);
    };

    __property void set_Celsius( short value) {
        m_value = (value*2+32);
    };
};

[JScript] 
public class Temperature implements IComparable {
    /// <summary>
    /// IComparable.CompareTo implementation.
    /// </summary>
    public function CompareTo(obj) : int {
        if(obj.GetType() == Temperature) {
            var temp : Temperature = Temperature(obj);

            return m_value.CompareTo(temp.m_value);
        }
        
        throw new ArgumentException("object is not a Temperature");    
    }

    // The value holder
    protected var m_value : ushort;

    public function get Value() : ushort {
        return m_value;
            }
            
            public function set Value(value : ushort) {                        
        m_value = value;
    }
}

[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

UInt16 Structure | UInt16 Members | System Namespace | Equals

Show: