xsharp.eu • How does this translate C#->X#
Page 1 of 1

How does this translate C#->X#

Posted: Tue Sep 27, 2022 9:21 pm
by ic2
This C# code:

Code: Select all

	public static void ProcessUITasks()
	// When called, forces the UI to redraw. 
	{
		DispatcherFrame frame = new DispatcherFrame();
		Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate (object parameter) {
			frame.Continue = false;
			return null;
		}), null);
		Dispatcher.PushFrame(frame);
	}
in ILSpy translates as:

Code: Select all

public static method ProcessUITasks() as void
	local frame as DispatcherFrame
	frame := DispatcherFrame{}
	Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ =>
		frame:Continue := false
		return null
	}, null)
	Dispatcher.PushFrame(frame)
This gives Error XS1593 Delegate 'System.Windows.Threading.DispatcherOperationCallback' does not take 0 arguments

Indeed delegate (object parameter) from the C# code is missing. But I am not sure how to do this in X#

Anyone an idea?

Dick

How does this translate C#->X#

Posted: Wed Sep 28, 2022 5:54 am
by Chris
Hi Dick,

This should do it, by supplying the parameter as the c# version of the code does:

Code: Select all

	Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ parameter AS OBJECT =>
		frame:Continue := FALSE
		RETURN NULL
	}, NULL)

How does this translate C#->X#

Posted: Wed Sep 28, 2022 6:54 pm
by ic2
Hello Chris,

Thanks, it works now.

I must confess that I have no idea how the syntax is supposed to work; any Lambda expressions or "delegates" in my program are "borrowed" and even now I see your working X# solution I do not understand how you got to this (gone is the delegate, added is the =>).

But main thing is that it works!

Dick

How does this translate C#->X#

Posted: Wed Sep 28, 2022 8:42 pm
by robert
Dick,

There are 2 ways to write this:
- A Lambda expression (with the => operator, like you did)
- With the DELEGATE keyword

Code: Select all

Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ DELEGATE ( p AS OBJECT )  {  frame:Continue := FALSE, NULL }, NULL)
The syntax for the DELEGATE expression is

Code: Select all

DELEGATE [ ( Parameters) ] { <Block> }
Where <Block> is :
- a single expression
- an expression list, seperated with commas. The last expression is the return value, like above. All on one line.
- a statement block (starts with a CRLF after the LCurly ({) and also has a CRLF before the RCurly (})