Partager via


Type Conversion Operations 

Visual J# does not support type conversion operators. A common language specification (CLS)-compliant type usually provides an alternate mechanism of achieving this conversion using the ToX, where X is the name of the target type, and FromY, where Y is the name of the source type, methods. The Visual J# user can perform the type conversions using these methods.

Direct use of the op_Implicit and op_Explicit methods is not recommended, as these methods can have overridden return types and therefore cannot be used for method resolution.

Example

// vjc_type_conv_op.jsl
import System.*;
class MyClass 
{
    public static void main(String [] args)
    {
        int i = 10;
        Decimal dec = new Decimal(20);
        Console.WriteLine("dec = {0}", dec);
        Console.Write("i = ");
        Console.WriteLine(i);

        // In Visual J#, you can use the CLS-compliant
        // conversion methods:
        i = Decimal.ToInt32(dec);
        Console.Write("i = ");
        Console.WriteLine(i);

        // Converting an int to a System.Decimal.
        // In Visual J#, you can use the constructor that 
        // takes int:
        i = 9;
        dec = new System.Decimal(i);
        Console.WriteLine("dec = {0}", dec);
    }
}

Output

dec = 20
i = 10
i = 20
dec = 9

See Also

Reference

Syntax for Targeting the .NET Framework