# Writing your own COM component using VB.NET

Note: Calling a COM component requires Analytica Enterprise release 4.6 or higher.

This article takes you through the steps of creating and writing your own COM component in VB.NET that can be called from Analytica. You can use your component to compute something and return the result, to interface with other applications, or to interact with the user.

To follow this example, you will also need to have

• Microsoft Visual Studio with VB (I am using Visual Studio 2013)
• .NET Framework 4.0 or higher (I am using .NET 4.5).

## Using the component

In this example, we will create a component that accepts text and speaks it using a voice synthesizer. You would use this from your Analytica Model as follows.

Variable Speaker := COMCreateObject("AnaVoice.Speaker")
Button RunIt
OnClick: LongComputation ; Speaker->Say("The simulation has finished")

## Creating the project

Steps:

1. Start Visual Studio.
To enable Visual Studio to register your component for you, you should launch it as Administrator. Right-click on the Visual Studio icon and select Run as administrator.
2. Select New Project...
3. Select Visual Basic + Windows + Class Library, and name your project (We've named this one AnaVoice).
4. From the menus, select Project / AnaVoice Properties / Compile.
Scroll down and check Register for COM interop
5. If you'll be using Analytica 64-bit, then from the menus select Build / Configuration Manager....
In the table, change Any CPU to <New...>. New platform should say x64. Press OK.
6. In the Solution Explorer window, right click on the Class1.vb filename and change it to AnaVoice.vb.

## Defining and registering a class

With the project set up, you are ready to define the component interface. In the code window for AnaVoice.vb, enter this code:

 

<ComClass(Speaker.ClassId, Speaker.InterfaceId, Speaker.EventsId)> Public Class Speaker Public Const ClassId As String = "3A42F85E-24C8-4BAA-91B5-AE56C4683C13" Public Const InterfaceId As String = "D99D7C79-2BA7-4A33-B7BC-9B7F19FDF828" Public Const EventsId As String = "CA128AC4-580C-4112-9EAD-8D1599E3F37A" Public Sub New() MyBase.New() End Sub Public Sub Say(ByVal phrase As String) End Sub End Class 

Note: In C#, you'll need to add
using System.Runtime.InteropServices;
at the top of your file.

When creating your own component, be sure to replace the GUIDs that are in green above with unique GUIDs. To generate unique GUIDs, select Create GUID from the Tools menu, and use the Registry Format option. After copy and pasting, remove the curly braces so it looks like the code above.

You now have a complete project and API. Compile it by selecting Build / Rebuild Solution. This will build the project and register the component.

To validate that the component was actually registered, you can run RegEdit.exe and navigate to

HKEY_CLASSES_ROOT\AnaVoice.Speaker

The method that you'll be calling from your Analytica model is

 Public Sub Say(ByVal phrase As String)
 End Sub

Notice that the parameter is marked as ByVal. This is important -- Analytica's COM facility can only call methods that use ByVal parameters only, so do not declare any of your parameters as ByRef.

## Implementing the Method

Now we'll implement the Say() method to actually pronounce the phrase. Of course, in your own custom classes, this in where you would be implementing the logic that you are interested in.

2. On the left, select Assemblies / Framework
3. In the main pane, scroll down and select System.Speech. Press OK
Note: If it isn't there, you don't have the .NET 4.0 or better Framework installed.
4. At the top of the AnaVoice.vb file, add these lines
Imports System.Speech
Imports System.Speech.Synthesis
5. Enter the code for the body of the method
    Public Sub Speak(ByVal phrase As String)
Dim synth As SpeechSynthesizer = New SpeechSynthesizer()
Dim prompt As Prompt = New Prompt(phrase)
synth.Speak(prompt)
End Sub


Your VB code is now complete. Compile it again (Build / Build Solution).

## Test it

1. Turn on your computer's speakers
2. Start Analytica, edit mode, bring diagram to front.
3. Create a variable named Speaker and set its definition to
COMCreateObject("AnaVoice.Speaker")
4. Evaluate to test that it works. If it works (your component is properly registered) the result will be «COM Object»
5. In another variable Definition or Button OnClick, call your method using
Speaker->Say("Some text to say")

You can use my test model, "AnaVoice test.ana" shown here