C# Equivalent of WithEvents?

B

Bradley Grant

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, but the problem may lie with the
following code that was written in Visual Basic, and I was
wondering what the C# equivalent of WithEvents is?

Any help/ideas would be appreciated.

'Declare an object variable to hold the device collection.
'We use WithEvents so that we can utilize the OnChange event.
Private WithEvents Connected As BDAQ.BBDaqEnum

'Declare an object variable to hold a device.
'We use WithEvents so that we can utilize the
OnDataAvailable event
'during timed sampling.
Private WithEvents Device1 As BDAQ.BBDaqDevice
 
F

Frank Oquendo

Bradley said:
and I was
wondering what the C# equivalent of WithEvents is?

There isn't an equivalent as it's not needed in C#. Events are simply
methods which accept delegates as arguments. So long as a routine within
your code has the correct signature, you can pass reference to that
routine and it will be invoked by that object when the event is fired.

Suppose you have an object with an Event named Click. Click's signature
looks like this:

System.EventHandler SomeObject.Click

All you have to do to handle that event is C# is this:

myObject.Click += new EventHandler(this.OnClick);

public void OnClick(object sender, EventArgs e)
{
// do something
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
C

Chris Taylor

Hi,

I do not know the library that you are using, but it appears to be a COM
library. If this is the case, the events exposed by the COM component
can be treated as C# events.

You can handle these events by adding a instance of a delegate to the
event that is exposed through the .NET wrapper that is generated when
referencing the COM component. For example to handle the OnChange event
there should be a delegate that matches the signature of the event and
you will have to declare a method with this same signature, this will be
the handler that is invoked when the event is fired. Then assuming the
event is called OnChange you would have some code that resembles the
following:

OnChange += new XXXEvents_OnChangeEventHandler( MyOnChangeHandler );

Please keep in mind that the references to actual names used in this
sample are not necessarily the names exposed by your component, they are
purely for the sake of the example.

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
B

Bradley Grant

Thanks for the info, I am relatively new to programming C#,
about a year or so now, with only a little Quick C in the
past, I maybe getting a little over my head. Here is the
device I am trying to write a program for.

http://www.bb-elec.com/product.asp?sku=UD128A8D&PathId=40&altsku=daqaichart

I have made a reference to a COM using Visual Studio 2002
under the Project/Add Reference, which created a couple of
dll files in the bin directory,

Interop.BDaq.dll, Interop.VBA.dll

I've included those as using statments.

using BDaq;
using VBA;

Added these in the public class Form1

public BDaq.BBDaqEnum Connected;
public BDaq.BBDaqDevice Device1;

Added the following instance in a Button Event.

BDaq.BBDaqEnum Connected = new BDaq.BBDaqEnum();

//and this is where it hands up, creates an error in the
compiler with either comment out.

An unhandled exception of type
'System.IO.DirectoryNotFoundException'

Device1 = Connected.Devices("DAQ1");
Device1.Open();

Thanks for all your ideas, both of you, it will take me a
while to digest what you said. Like I said, I may be
getting a little over my head, this is more or less a
hobby, but could amount to something done the road, maybe.

I like the binary statement 01 = 1, 10 = 2, 11 = 3, 100 =
4, in binary of course.
 
C

Chris Taylor

Hi,

I can not see from your code why you are getting the exception. What I
would recommend is that you place a try/catch block around the offending
code and catch the exception, if you do something like the following,
you can capture the exception message in the Output window and the post
it, maybe that will give some insight into the problem.

try
{
BDaq.BBDaqEnum Connected = new BDaq.BBDaqEnum();
Device1 = Connected.Devices("DAQ1");
Device1.Open();
}
catch ( Exception ex )
{
System.Diagnostics.Debug.WriteLine( ex.ToString() );
}

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
 
B

Bradley Grant

I would like to get some C# example programs, but the
vendor was only aware of Visual Basic examples, the
included software works fine with the device, only a little
limited, so I thought I would try to make my own C#
program, there just doesn't seem to be much of this out
there, even on the Internet, Oh well such as life, I'll
just keep plugging along as I get some spare time. Thanks
for your info.
 

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


Top