WithEvents question

  • Thread starter Thread starter Yoavo
  • Start date Start date
No, there is not an equivalent. In C# if you want to raise events from
outside the class you will need to define public methods in the class which
raise events. (I'm assuming from VB examples that I've seen that using
'WithEvents' allows access to private methods which raise events, but I
stand to be corrected on this.)
 
Hi,
I tried to implement the events delegation using "UCOMIConnectionPoint":

/////////////////////////////////////////////////////////////////
UCOMIConnectionPoint mCnnctPt;
int mCookie;
UCOMIConnectionPointContainer CnnctPtCont =
(UCOMIConnectionPointContainer)MyApp;
MyAppEvents PES = new MyAppEvents();
Guid guid = new Guid("44E333BB-F4FA-11D3-B77C-001111CCAE9F");
CnnctPtCont.FindConnectionPoint(ref guid, out mCnnctPt);
mCnnctPt.Advise(PES, out mCookie);
/////////////////////////////////////////////////////////////////

when "MyAppEvents" is a class which inherits from an event interface.
This worked when the client (c# program) was inproc. (written as a DLL).
but it did not work when the client program was out-of-proc (written as
EXE).

Is there a problem of using UCOMIConnectionPoint out-of-proc ?
 
when "MyAppEvents" is a class which inherits from an event interface.
This worked when the client (c# program) was inproc. (written as a DLL).
but it did not work when the client program was out-of-proc (written as
EXE).

Is there a problem of using UCOMIConnectionPoint out-of-proc ?

But... what is the problem of the normal C# event handling?

Object.Event += new EventHandler(...);

?
 
Yoavo said:
Hi,
Is there an equivalent code for "WithEvents" (of VB6) in CSharp ?
There is no WithEvents in C#; But you can add eventhandler to an event.

Object.Event += new EventType(this.Method);
(EventType must be replaced by the exact type of the event)

or since C# 2.0

Object.Event += this.Method;

In the form designer you mark the control and then in the propertiegrid
click on the flash icon. There you'll see a list of the events of the
control. If you double click on an event, an eventhandler will be created
(if not already exists) and the code of the eventhandler will be shown.
You also can enter a handler name (if you prefer to name the handler
yourself) or select an existing handler from the drop down list.

hth
Christof
 
To make things clearer:
I have a COM ATL server application (which was written several years ago)
and fires events using "IConnectionPointContainer" interface.
(I do not want to rewrite the server application in a .NET language.)
Now I have clients application that uses the server and want to get the
events from it.
In VB6 client, I use "WithEvents" and everything works OK.
I tried to write a C# client (using UCOMIConnectionPoint), but I do not get
the event.
Using UCOMIConnectionPoint worked for me before (in a different project),
but in this case the C# client was InProc.

So this is what leads to my question:
Is there a problem using UCOMIConnectionPoint OutOfProc ?
 
Use the standard C# sytax that was mentioned...

MyObject.MyEvent += new MyObject.MyEventDelegate(MyEventHandler);

If your out of process DCOM exe uses connection points and such, this will
hook up to your default event interface in your coclass. It does use the
ConnectionPointContainer then creates a connection point and calls Advise
all for you behind the scenes. The only problem I have found is when
calling a non-default connection point interface. VB only deals with the
default event interface and only if it is IDispatch based so if it worked in
VB, it ought to work fine with the above syntax. As for the non-default
connection points, how often does anyone actually do this? If for some
reason this is your situation, I'd suggest making a second object, say
"MyObject2" with the non-default connection point and not support two event
interfaces on the same object.

Good luck.
 

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

Similar Threads

COM events in CSharp 3
How to do "WithEvents" in C#? 11
WithEvents 6
C# Equivalent to VB's WithEvents? 2
C# Equivalent of WithEvents? 5
WithEvents in C#.NET 4
VB6 WithEvents in C#? 2
Friend WithEvents 2

Back
Top