473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

WithEvents question

Hi,
Is there an equivalent code for "WithEvents" (of VB6) in CSharp ?

Yoav.
Jul 23 '07 #1
6 1827
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.)

"Yoavo" <yo**@cimatron.co.ilwrote in message
news:ei****************@TK2MSFTNGP05.phx.gbl...
Hi,
Is there an equivalent code for "WithEvents" (of VB6) in CSharp ?

Yoav.

Jul 23 '07 #2
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 ?

"Clive Dixon" <clived at digita dot comwrote in message
news:ek**************@TK2MSFTNGP04.phx.gbl...
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.)

"Yoavo" <yo**@cimatron.co.ilwrote in message
news:ei****************@TK2MSFTNGP05.phx.gbl...
>Hi,
Is there an equivalent code for "WithEvents" (of VB6) in CSharp ?

Yoav.


Jul 23 '07 #3
"Yoavo" <yo**@cimatron.co.ilha scritto nel messaggio
news:eZ**************@TK2MSFTNGP02.phx.gbl...
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(...);

?

--
Help The New .Net Site! http://www.devbox4.net
Jul 23 '07 #4
"Yoavo" <yo**@cimatron.co.ilschrieb im Newsbeitrag
news:ei****************@TK2MSFTNGP05.phx.gbl...
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
Jul 23 '07 #5
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 ?
"Fabio" <zn*******@virgilio.itwrote in message
news:OL*************@TK2MSFTNGP06.phx.gbl...
"Yoavo" <yo**@cimatron.co.ilha scritto nel messaggio
news:eZ**************@TK2MSFTNGP02.phx.gbl...
>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(...);

?

--
Help The New .Net Site! http://www.devbox4.net

Jul 24 '07 #6
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.

Jul 24 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
2
by: Jakob Bengtsson | last post by:
Hi, I have a form (which cannot be serialized). In the form's code I declare an object like this (never mind the object nor class name, it's for illustration only): Private WithEvents...
5
by: Bradley Grant | last post by:
I am trying to write a program to access a device through USB port, there is some Visual Basic program examples that work with the device, but I am trying to write a program using C#, no luck yet,...
2
by: Peter | last post by:
Hello, Thanks for reviewing my question. I am following KB article about creating a custom control and its source code is in VB. I got to a part where I have the following: Private...
11
by: Brett | last post by:
What is the C# equivalent for this VB.NET code: Private WithEvents IE_Inst As New SHDocVw.InternetExplorer I get as far as: private SHDocVw.InternetExplorer IE_Inst = new...
2
by: Benign Vanilla | last post by:
I am a ASP.NET newbie so please bear with me. I had a series of webforms working on my web site, and then I made some tragic error that is causing all kinds of problems. The main symptom is that...
0
by: Steve | last post by:
Hello, I have been experimenting/learning to use aspx with and without VS2003. In VS 2003 I created a simple webform/application which contains a listbox and a label. I populate the listbox...
4
by: Shapper | last post by:
Hello, I have an aspx.vb page with this code: Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Menu1 As MenuControl.Web.UI.Menu ... If I have my code in the aspx...
6
by: | last post by:
Is there any additional overhead for declaring a variable using the WithEvents keyword even if none of the events are being handled?
8
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored? Or, another way to phrase the question,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.