Question

I am trying to implement a method based on Cocoa's

- (void) alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;

The problem is the parameter of the type void*. I don't know what the C# equivalent is for pointer to void.

I tried unsafe execution with "void*" but apparently that's not the same. I also tried simply "void" but the C# compiler claims that void cannot be used as a parameter type.

Any ideas?

Error CS0123: A method or delegate `MyProgram.MainWindow.alertDidEnd(MonoMac.AppKit.NSAlert, int, MonoMac.Foundation.NSObject)' parameters do not match delegate `MonoMac.Foundation.NSAction()' parameters (CS0123)

Update (after problem was resolved):

These are the call and the method that made this work:

alert.BeginSheet ((NSWindow)sender, this, new Selector ("alertDidEnd:returnCode:contextInfo:"), IntPtr.Zero);


[Export("alertDidEnd:returnCode:contextInfo:")]
public void alertDidEnd (NSAlert alert, int result, IntPtr ci)
{}

The first is displaying the alert attached to the window, the second is the method declaration of the function called when the alert is dismissed. The result is stored in int result.

Was it helpful?

Solution

void* maps to IntPtr in .net:

[Export("alertDidEnd:returnCode:contextInfo:")] 
public void alertDidEnd (NSAlert alert, int returnCode, IntPtr contextInfo) 
{ 
    Console.WriteLine (returnCode); 
}

You can take a look here for a working example:

https://github.com/picoe/Eto/blob/master/Source/Eto.Platform.Mac/Forms/MacModal.cs

Also, it looks like from your error message that you should just use a plain parameterless method for whatever you're using it with. NSAction is a delegate with no parameters.

OTHER TIPS

You can define the method as:

[Export("alertDidEnd:returnCode:contextInfo:")] 
public void alertDidEnd (NSAlert alert, int returnCode, NSObject contextInfo) 
{ 
    Console.WriteLine (returnCode); 
}

The void * pointer will be wrapped into a NSObject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top