Export (0) Print
Expand All

Decimal.Equals Method (Object)

Returns a value indicating whether this instance and a specified Object represent the same type and value.

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

'Declaration
Public Overrides Function Equals ( _
	value As Object _
) As Boolean
'Usage
Dim instance As Decimal
Dim value As Object
Dim returnValue As Boolean

returnValue = instance.Equals(value)
public boolean Equals (
	Object value
)
public override function Equals (
	value : Object
) : boolean

Parameters

value

An Object.

Return Value

true if value is a Decimal and equal to this instance; otherwise, false.

The following code example compares several Decimal and other objects to a reference Decimal value using the Equals method.

' Example of the Decimal.CompareTo and Decimal.Equals instance methods.
Imports System
Imports Microsoft.VisualBasic

Module DecCompToEqualsObjDemo
    
    ' Get the exception type name; remove the namespace prefix.
    Function GetExceptionType( ex As Exception ) As String

        Dim exceptionType   As String = ex.GetType( ).ToString( )
        Return exceptionType.Substring( _
            exceptionType.LastIndexOf( "."c ) + 1 )
    End Function

    ' Compare the Decimal to the Object parameters, 
    ' and display the Object parameters with the results.
    Sub CompDecimalToObject( Left as Decimal, Right as Object, _
        RightText as String )

        Console.WriteLine( "{0,-46}{1}", "Object: " & RightText, _
            Right )
        Console.WriteLine( "{0,-46}{1}", "Left.Equals( Object )", _
            Left.Equals( Right ) )
        Console.Write( "{0,-46}", "Left.CompareTo( Object )" )

        ' Catch the exception if CompareTo( ) throws one.
        Try
            Console.WriteLine( "{0}" & vbCrLf, _
                Left.CompareTo( Right ) )
        Catch ex As Exception
            Console.WriteLine( "{0}" & vbCrLf, _
                GetExceptionType( ex ) )
        End Try
    End Sub

    Sub Main( )
        Console.WriteLine( _
            "This example of the Decimal.Equals( Object ) " & _
            "and " & vbCrLf & "Decimal.CompareTo( Object ) " & _
            "methods generates the " & vbCrLf & _
            "following output. It creates several different " & _
            "Decimal " & vbCrLf & "values and compares them " & _
            "with the following reference value." & vbCrLf )

        ' Create a reference Decimal value.
        Dim Left as New Decimal( 987.654 )

        Console.WriteLine( "{0,-46}{1}" & vbCrLf, _
            "Left: Decimal( 987.654 )", Left )

        ' Create objects to compare with the reference.
        CompDecimalToObject( Left, New Decimal( 9.8765400E+2 ), _
            "Decimal( 9.8765400E+2 )" )
        CompDecimalToObject( Left, 987.6541D, "987.6541D" )
        CompDecimalToObject( Left, 987.6539D, "987.6539D" )
        CompDecimalToObject( Left, _
            New Decimal( 987654000, 0, 0, false, 6 ), _
            "Decimal( 987654000, 0, 0, false, 6 )" )
        CompDecimalToObject( Left, 9.8765400E+2, _
            "Double 9.8765400E+2" )
        CompDecimalToObject( Left, "987.654", _
            "String ""987.654""" )
    End Sub
End Module 

' This example of the Decimal.Equals( Object ) and
' Decimal.CompareTo( Object ) methods generates the
' following output. It creates several different Decimal
' values and compares them with the following reference value.
' 
' Left: Decimal( 987.654 )                      987.654
' 
' Object: Decimal( 9.8765400E+2 )               987.654
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: 987.6541D                             987.6541
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      -1
' 
' Object: 987.6539D                             987.6539
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      1
' 
' Object: Decimal( 987654000, 0, 0, false, 6 )  987.654000
' Left.Equals( Object )                         True
' Left.CompareTo( Object )                      0
' 
' Object: Double 9.8765400E+2                   987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException
' 
' Object: String "987.654"                      987.654
' Left.Equals( Object )                         False
' Left.CompareTo( Object )                      ArgumentException

// Example of the decimal.CompareTo and decimal.Equals instance 
// methods.
import System.* ;

class DecCompToEqualsObjDemo
{
       // Get the exception type name; remove the namespace prefix.
    public static String GetExceptionType(System.Exception ex)
    {
        String exceptionType = ex.GetType().ToString();
        return exceptionType.Substring((exceptionType.LastIndexOf('.') + 1));
    } //GetExceptionType   
   
    // Compare the decimal to the object parameters, 
    // and display the object parameters with the results.
    public static void CompDecimalToObject(System.Decimal left, 
        Object right, String rightText)
    {
        Console.WriteLine("{0,-46}{1}", "object: " + rightText, right);
        Console.WriteLine("{0,-46}{1}", "Left.Equals( object )",
            System.Convert.ToString(left.Equals(right)));
        Console.Write("{0,-46}", "Left.CompareTo( object )");
        try {
            // Catch the exception if CompareTo( ) throws one.
            Console.WriteLine("{0}\n", 
                System.Convert.ToString(left.CompareTo(right)));
        }
        catch (System.Exception ex)    {
            Console.WriteLine("{0}\n", GetExceptionType(ex));
        }
    } //CompDecimalToObject
   
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of the decimal.Equals( object ) and \n"
            + "decimal.CompareTo( object ) methods generates the \n"
            + "following output. It creates several different "
            + "decimal \nvalues and compares them with the following " 
            + "reference value.\n"));

        // Create a reference decimal value.
        System.Decimal left = new System.Decimal(987.654D);

        Console.WriteLine("{0,-46}{1}\n", "Left: decimal( 987.654 )", left);

        // Create objects to compare with the reference.
        CompDecimalToObject(left, new System.Decimal(987.654),
            "decimal( 9.8765400E+2 )");
        CompDecimalToObject(left, System.Convert.ToDecimal(987.6541), 
            "987.6541D");
        CompDecimalToObject(left, System.Convert.ToDecimal(987.6539),
            "987.6539D");
        CompDecimalToObject(left, new Decimal(987654000, 0, 0, false, 
            System.Convert.ToByte(6)), "decimal( 987654000, 0, 0, false, 6 )");
        CompDecimalToObject(left, (System.Double)9.8765400E+2, 
            "Double 9.8765400E+2");
        CompDecimalToObject(left, "987.654", "String \"987.654\"");
    } //main
} //DecCompToEqualsObjDemo

/*
This example of the decimal.Equals( object ) and
decimal.CompareTo( object ) methods generates the
following output. It creates several different decimal
values and compares them with the following reference value.

Left: decimal( 987.654 )                      987.654

object: decimal( 9.8765400E+2 )               987.654
Left.Equals( object )                         True
Left.CompareTo( object )                      0

object: 987.6541D                             987.6541
Left.Equals( object )                         False
Left.CompareTo( object )                      -1

object: 987.6539D                             987.6539
Left.Equals( object )                         False
Left.CompareTo( object )                      1

object: decimal( 987654000, 0, 0, false, 6 )  987.654000
Left.Equals( object )                         True
Left.CompareTo( object )                      0

object: Double 9.8765400E+2                   987.654
Left.Equals( object )                         False
Left.CompareTo( object )                      ArgumentException

object: String "987.654"                      987.654
Left.Equals( object )                         False
Left.CompareTo( object )                      ArgumentException
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft