To create the implementation class with COM attributes

In this section, you create an implementation class that implements the IExchangeUfl interface, and then configure that class with COM attributes.

  1. In Solution Explorer, right-click the bold project name that is in bold type, point to Add, and then click Class.

  2. In the Name field, enter "ExchangeUfl", and then click Add.

  3. Modify the class signature to implement the IExchangeUfl interface.

    Public Class ExchangeUfl : Implements IExchangeUfl
    End Class
    
    public class ExchangeUfl : IExchangeUfl
    {
    }
    
  4. Above the class declaration, add the following using/Imports statement:

    Imports System.Runtime.InteropServices
    
    using System.Runtime.InteropServices;
    
  5. From the Tools menu, click Create GUID.

  6. In the Create GUID dialog box, in the GUID format panel, select Registry Format.

  7. Click New GUID.

  8. Click Copy, and then close the dialog box.

  9. Above the class declaration, create an attribute with three values: ComVisible, ClassInterface and Guid (in C#) or GuidAttribute (in Visual Basic) with parameter string quotes.

> [!NOTE]
> <P>In Visual Basic, for readability add an underscore after the close tag to place the class on the next line.</P>


``` vb
<ComVisible(), ClassInterface(), GuidAttribute("")> _
Public Class ExchangeUfl : Implements IExchangeUfl
End Class
```

``` csharp
[ComVisible(), ClassInterface(), Guid("")]
public class ExchangeUfl : IExchangeUfl
{
}
```
  1. Enter a parameter value of "True" [Visual Basic] or "true" [C#] into the ComVisible attribute.

  2. Enter a parameter value of ClassInterfaceType.None from the ClassInterfaceType enum into the ClassInterface attribute.

  3. Paste the GUID value from the clipboard into the parameter value of the Guid attribute. Be careful to remove the curly braces and any carriage returns.

    Note

    Do not use the GUID that is provided in this code snippet. Create a unique GUID for your class.

    <ComVisible(True), ClassInterface(ClassInterfaceType.None),
    GuidAttribute("F5DCE88F-AD38-4a9a-9A69-0F8DC0EDB4E3")> _
    Public Class ExchangeUfl : Implements IExchangeUfl
    End Class
    
    [ComVisible(true), ClassInterface(ClassInterfaceType.None),
    Guid("F5DCE88F-AD38-4a9a-9A69-0F8DC0EDB4E3")]
    public class ExchangeUfl : IExchangeUfl
    {
    }
    
  4. Within ExchangeUfl, fulfill the contractual method signature from the interface with a public method.

> [!NOTE]
> <P>This will be the name of the function that is exposed in the embedded Crystal Reports Designer.</P>


``` vb
Public Function ConvertUSDollarsToCDN1(ByVal usd As Double) As
Double Implements IExchangeUfl.ConvertUSDollarsToCDN
End Function
```

``` csharp
public double ConvertUSDollarsToCDN(double usd)
{
}
```
  1. Within the method, create a conditional block that checks whether the usd method parameter is greater than Double.MaxValue and throws an exception if it is.

    Note

    You need to perform this check to prevent the risk of an overflow.

    If usd > Double.MaxValue Then
       Throw New Exception("Value submitted is larger than the maximum value allowed for a double.")
    End If
    
    if(usd > Double.MaxValue)
    {
       throw new Exception("Value submitted is larger than the maximum value allowed for a double.");
    }
    
  2. Following the conditional block, return the usd method parameter multiplied by an exchange rate of 1.45.

In this tutorial, only a simple math calculation is performed. However, any code could be placed within this method. For example, you could retrieve an exchange rate from a bank web service, to calculate the exchange rate dynamically within your report.

``` vb
Return (usd * 1.45)
```

``` csharp
return (usd * 1.45);
```
  1. From the File menu, click Save All.

  2. Close the class window.