Pass arg by reference in C++ event?

T

Todd

Is it possible to pass an argument by reference through
an event in C++ managed code? For example, can I do the
following?

public __gc class MyClass : public Control
{
public:

__event void MyEvent(bool &Cancel);

};


The compiler seems happy with this, and it goes ahead and
creates a DLL fine and dandy.

However, when I reference this DLL from my VB.NET
project, and attempt to do this:

Public WithEvents MyObject as MyClass

It causes the development environment to pop up a dialog
box saying "The operation could not be completed" the
moment I press Enter after typing the above line.

If I attempt to compile or run the code, I get this
error: "Visual Basic .NET compiler is unable to recover
from the following error: System Error &Hc0000005&
(Visual Basic internal compiler error). Save your work
and restart Visual Studio .NET."

If I remove the reference (&) from the event declaration
in the C++ project, then the VB.NET project works fine
(except I need to pass that argument by reference for a
working solution). It took me two days, through trial
and error, to determine it was the argument-by-reference
in that one event that was causing this error.

I also attempted a sample VB.NET project that defines an
event, passing an argument ByRef. This time, I was able
to include that DLL wihtout any problems.

' This class, defined in VB.NET works fine
Public Class MyVBClass
Public Event MyVBEvent(ByRef Cancel As Boolean)
End Class


Any thoughts? Is this just a .NET Development studio
bug, or is it really impossible to pass an argument by
reference through an event in C++ managed code?

Microsoft Development Environment 2003, Version 7.1.3008
Microsoft .NET Framework 1.1, Version 1.1.4322

I can work around the problem (sigh), by creating another
class that holds the "Cancel" boolean argument inside
that class and pass that class as a (by val) argument
through the event, but I really don't want to kludge up
my code that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top