By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Pass arg by reference in C++ event?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
__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

P: n/a
__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

P: n/a
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

P: n/a
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.