CodeBlock from C#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
JoeD
Posts: 10
Joined: Wed Apr 09, 2025 12:59 pm
Location: Deutschland

CodeBlock from C#

Post by JoeD »

Hi everyone,

I'm working on a C# unit test where I use a dbSysDat instance derived from dbShellServer. When the underlying DBF file is missing, the VO.dbServer throws an error and displays a modal dialog.

From what I understand, this modal dialog is the default behavior and can be overridden by assigning a custom ErrorBlock. This seems to be straightforward in X#, but I'm unsure how to create and pass a CodeBlock from C# to the ErrorBlock function.

My goal is to handle the error in one of the following ways:

Log the error message to the test output
Throw it as a regular exception
Or suppress it entirely

Is it possible to achieve this from C#? If so, how would I go about creating a compatible CodeBlock or delegate?
Thanks in advance for your help!

Best regards,
JoeD
User avatar
wriedmann
Posts: 4017
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: CodeBlock from C#

Post by wriedmann »

Hi Joe,
I would create an X# DLL in VO dialect since you need the runtime and do all from there.
The X# DLL should expose only native .NET types to the C# application.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
JoeD
Posts: 10
Joined: Wed Apr 09, 2025 12:59 pm
Location: Deutschland

Re: CodeBlock from C#

Post by JoeD »

Hi Wolfgang.

Thank you for your suggestion.
I'm not sure if it would be feasible for my approach to have a X#-DLL that exposes only native .NET types.

In the meantime I found the Macro Compiler and I use it like this to create the CodeBlock and redirect the ErrorHandler to my own Method.

Code: Select all

		
public static void SetErrorHandler()
{
	var macroCompiler = new MacroCompiler();
	var codeBlock = macroCompiler.CompileCodeblock("{ |e| MyNamespace.MyClass.ErrorHandler(e)}");
	XSharp.RT.Functions.ErrorBlock(codeBlock);
}

public static void ErrorHandler(__Usual x)
{
	if (x.Value is Error e)
	{
		throw new Exception(e.Message, e);
	}

	throw new Exception("Unerwarteter Fehler: " + x.ToString());
}
User avatar
wriedmann
Posts: 4017
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: CodeBlock from C#

Post by wriedmann »

Hi Joe,
a C# application can work only with native .NET datatypes.
Variables with VO datatypes would be seen as instances of X# classes.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply