1

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.

Andrew J. Brehm
  • 4,448
  • 8
  • 45
  • 70

2 Answers2

1

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.

Curtis
  • 1,552
  • 15
  • 20
  • Ta, I'll try that out. (If I used a parameterless method, I wouldn't know which button was pressed in the message box.) – Andrew J. Brehm May 10 '12 at 22:31
  • It might help if you posted what you are actually trying to do, since it seems that you are not using the NSAlert correctly.. e.g. if you check the return value of myAlert.RunModal(), that will give you the return code as well. – Curtis May 11 '12 at 15:17
  • I thought RunModal() was an alternative to beginSheetModalForWindow(). I didn't think I was supposed to call both of them. – Andrew J. Brehm May 11 '12 at 19:35
  • It is, though you didn't mention that you were using BeginSheetModalForWindow(), or provide any code on how you are calling it - it might help identify the issue.. – Curtis May 11 '12 at 23:21
  • In all Cocoa tutorials I found the Begin...() method was always used when the AlertDidEnd() method was also required. I thought they belonged together. – Andrew J. Brehm May 11 '12 at 23:28
  • What I'm saying is the problem might not be the way you are implementing AlertDidEnd, but rather how you are calling the Begin…() methods.. – Curtis May 12 '12 at 18:17
  • So you're saying your problem is resolved? The code above is not the reason for your compile error.. – Curtis May 13 '12 at 17:03
0

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.

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81