Custom Class Demo

For this demo we will show you how to construct it from scratch. The demo is also available as a completed demo in the demo repository on Codeplex .

New .NET Class Library

Using Visual Studio, create a new class library. The class library name and class name is up to you. For most of our demos we use “Right” (the .NET portion) and “Left” (the Delphi portion). “Middle” is the CrossTalk library itself.

The Class

By default, a class named Class1 will be created. You can leave it alone, or rename it. Add a method. For example:

Now we have a functional, albeit simple, .NET class that can be called from Delphi using CrossTalk. Delphi strings and .NET strings are very different, but CrossTalk will handle all of the conversions for us.

Build the Project

Build the project. This is important not only to make sure our code is OK, but the CrossTalk Project Manager will need to use the output of this project.

Delphi Project

If you have an existing Delphi project that will use the .NET class you can skip this step. Otherwise create a shell Delphi project and save it. For this demo we will assume it is a VCL forms application. CrossTalk however works in packages, DLLs, console applications, and more.

CrossTalk Project

Select Project, CrossTalk Project Settings. A browse dialog will appear, select the directory where your Delphi project file exists. This can be a different directory if you plan to share the library among several projects, but for now it will simplify search path issues. Select a new filename such as CustomClass.ctp.

Click Add User. This adds a reference to a user assembly. Navigate to the output directory of the .NET project and select the assembly (DLL file).

Select the assembly, and then click Add. Select the class by name.

Click OK.

Click Save, and then close the window.

Return to Delphi and add CTHelloWorld to the uses clause of the unit that you want to use the .NET class in. In our case, Unit1.pas. If the CTP file exists in the same directory as the project file, Delphi will find the CrossTalk generated units without any need to explicitly add them or modify the search path.

Now we can write code to use our .NET class. Here is some example code:

Class1 is the name of the class, because we never renamed it from the default.