469,600 Members | 2,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Pass arg by reference in C++ event?

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.
Jul 21 '05 #1
4 4292
__event void MyEvent(bool &Cancel);


bool __gc *Cancel

or

Boolean *Cancel

should work.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #2
__event void MyEvent(bool &Cancel);


bool __gc *Cancel

or

Boolean *Cancel

should work.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 21 '05 #3
Yes, that did it! THANK YOU!

-----Original Message-----
__event void MyEvent(bool &Cancel);


bool __gc *Cancel

or

Boolean *Cancel

should work.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Jul 21 '05 #4
Yes, that did it! THANK YOU!

-----Original Message-----
__event void MyEvent(bool &Cancel);


bool __gc *Cancel

or

Boolean *Cancel

should work.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Frank Rizzo | last post: by
7 posts views Thread by John | last post: by
2 posts views Thread by Todd | last post: by
3 posts views Thread by sloan | last post: by
24 posts views Thread by =?Utf-8?B?U3dhcHB5?= | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.