473,406 Members | 2,633 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,406 software developers and data experts.

VB .NET -> C# conversion problem

Hi!

I try to convert some VB.NET 2003 code into C# 2003 code. Code uses
Sax.Communications COM-port control, and VB code is working fine.

Here is part of VB Class code I'm trying to convert...

Imports Sax.Communications '// Importing component Namespace

Private WithEvents _ComPort As SerialConnection '// Declaration Com-port
as Class Variable

_ComPort = New SerialConnection '// Creating instance, setup Com settings
_ComPort.Open() '// and open port for use

Private Sub _ComPort_DataAvailable(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles _ComPort.DataAvailable '// Event Handling
if data will be available
....
'// This sub saves available data into "ComBufferString"-property,
'// and it Raises there Event like
'// RaiseEvent ComBufferChanged(Me, New EventArgs)
....
End Sub

....and as C# code something like...

using Sax.Communications;

private SerialConnection _ComPort; // <- How to convert this like VB.NET
"WithEvents" ???

_ComPort = new SerialConnection();
_ComPort.Open();

private void _ComPort_DataAvailable(System.Object sender,
System.EventArgs e)
{
....
// This sub saves available data into "ComBufferString"-property,
// and it Raises there Event like
//if (ComBufferChanged != null) ComBufferChanged(this, new EventArgs());
....
}

My problem is: private void _ComPort_DataAvailable is never fired when
Com port has received data from device attached into COM-port :(

Propably VB code line...

Private WithEvents _ComPort As SerialConnection

.... is not correctly converted into C# as ...

private SerialConnection _ComPort;

or am I missing something?

--
Thanks in advance!

Mika
Nov 17 '05 #1
3 2444
Hi Mike M,
in C# there is no need for a withevents keyword. Instead of using the
handles keyword you will need to assign a handler function using the +=
syntax.

In your example _ComPort instance will have an event called DataAvailable,
in the load event or constructor you need to assign the function that will
handle this event using some code like:
_ComPort.DataAvailable += new
DataAvailableEventHandler(_ComPort_DataAvailable);

I am not sure what your delegate is called (i.e. the thing I called
DataAvailableEventHandler in my example above) you will have to look at the
documentation or if you are using and IDE it will probably fill it in for
your.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Mika M" wrote:
Hi!

I try to convert some VB.NET 2003 code into C# 2003 code. Code uses
Sax.Communications COM-port control, and VB code is working fine.

Here is part of VB Class code I'm trying to convert...

Imports Sax.Communications '// Importing component Namespace

Private WithEvents _ComPort As SerialConnection '// Declaration Com-port
as Class Variable

_ComPort = New SerialConnection '// Creating instance, setup Com settings
_ComPort.Open() '// and open port for use

Private Sub _ComPort_DataAvailable(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles _ComPort.DataAvailable '// Event Handling
if data will be available
....
'// This sub saves available data into "ComBufferString"-property,
'// and it Raises there Event like
'// RaiseEvent ComBufferChanged(Me, New EventArgs)
....
End Sub

....and as C# code something like...

using Sax.Communications;

private SerialConnection _ComPort; // <- How to convert this like VB.NET
"WithEvents" ???

_ComPort = new SerialConnection();
_ComPort.Open();

private void _ComPort_DataAvailable(System.Object sender,
System.EventArgs e)
{
....
// This sub saves available data into "ComBufferString"-property,
// and it Raises there Event like
//if (ComBufferChanged != null) ComBufferChanged(this, new EventArgs());
....
}

My problem is: private void _ComPort_DataAvailable is never fired when
Com port has received data from device attached into COM-port :(

Propably VB code line...

Private WithEvents _ComPort As SerialConnection

.... is not correctly converted into C# as ...

private SerialConnection _ComPort;

or am I missing something?

--
Thanks in advance!

Mika

Nov 17 '05 #2
Well... I tried to add line...

_ComPort.DataAvailable += new
DataAvailableEventHandler(_ComPort_DataAvailable);

....into my class constructor, but got error...

"The type or namespace name 'DataAvailableEventHandler' could not be
found (are you missing a using directive or an assembly reference?)"

Then I tried to change it to...

_ComPort.DataAvailable += new EventHandler(_ComPort_DataAvailable);

....but it caused error too...

"Cannot implicitly convert type 'System.EventHandler' to
'Sax.Communications.SerialConnection.DataAvailable EventHandler'"

Maybe I'm missing something, or placed the line into wrong place. I'm
quite newbie with C#, used mostly VB.NET about since summer 2003.

I don't understand much about delegates, mostly coded them "try with
luck" method :)

Any suggestions?

Mark R. Dawson wrote:
Hi Mike M,
in C# there is no need for a withevents keyword. Instead of using the
handles keyword you will need to assign a handler function using the +=
syntax.

In your example _ComPort instance will have an event called DataAvailable,
in the load event or constructor you need to assign the function that will
handle this event using some code like:
_ComPort.DataAvailable += new
DataAvailableEventHandler(_ComPort_DataAvailable);

I am not sure what your delegate is called (i.e. the thing I called
DataAvailableEventHandler in my example above) you will have to look at the
documentation or if you are using and IDE it will probably fill it in for
your.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"Mika M" wrote:

Hi!

I try to convert some VB.NET 2003 code into C# 2003 code. Code uses
Sax.Communications COM-port control, and VB code is working fine.

Here is part of VB Class code I'm trying to convert...

Imports Sax.Communications '// Importing component Namespace

Private WithEvents _ComPort As SerialConnection '// Declaration Com-port
as Class Variable

_ComPort = New SerialConnection '// Creating instance, setup Com settings
_ComPort.Open() '// and open port for use

Private Sub _ComPort_DataAvailable(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles _ComPort.DataAvailable '// Event Handling
if data will be available
....
'// This sub saves available data into "ComBufferString"-property,
'// and it Raises there Event like
'// RaiseEvent ComBufferChanged(Me, New EventArgs)
....
End Sub

....and as C# code something like...

using Sax.Communications;

private SerialConnection _ComPort; // <- How to convert this like VB.NET
"WithEvents" ???

_ComPort = new SerialConnection();
_ComPort.Open();

private void _ComPort_DataAvailable(System.Object sender,
System.EventArgs e)
{
....
// This sub saves available data into "ComBufferString"-property,
// and it Raises there Event like
//if (ComBufferChanged != null) ComBufferChanged(this, new EventArgs());
....
}

My problem is: private void _ComPort_DataAvailable is never fired when
Com port has received data from device attached into COM-port :(

Propably VB code line...

Private WithEvents _ComPort As SerialConnection

.... is not correctly converted into C# as ...

private SerialConnection _ComPort;

or am I missing something?

--
Thanks in advance!

Mika

Nov 17 '05 #3
You've only got a using statement for the namespace Sax.Communications
but you're trying to use DataAvailableEventHandler from the namespace
Sax.Communications.SerialConn*ection.
Try:
_ComPort.DataAvailable += new
Sax.Communications.SerialConn*ection.DataAvailable EventHandler(_Com*Port_DataAvailable);
or just:
_ComPort.DataAvailable += new
SerialConn*ection.DataAvailableEventHandler(_Com*P ort_DataAvailable);

Nov 17 '05 #4

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

Similar topics

11
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
5
by: ann | last post by:
Does somebody know why I get a blank string in strA? Last time post the wrong code. " Option Strict On Option Explicit On Public Class Cast Private Sub FuncA()
4
by: Matthias Hullin | last post by:
Hi! On http://festest.hullin.net/index2.php?was=nurstruktur&ausklapp=,2, (make sure to add that last comma to the URL), you can see a basic example of a discussion board's tree structure. Right...
0
by: John Lafrowda | last post by:
Dear all, here is a simple problem that I cannot overcome: I'm trying to write a client/server application in Visual Basic .net. The server is an executable (.exe) project, the clients are class...
1
by: Michael Tissington | last post by:
I'm trying to convert a project from VS2003 to VS2005 After conversion all of my TagPrefix are not recognized in the body. <%@ Register TagPrefix="Oaklodge" TagName="Curve"...
0
by: egbert.beuker | last post by:
Hi, I encountered a conversion problem in my .net web app (c#), and I hope someone can help me: I'm working on a generic way to store data in a database with a few generated classes. I want...
0
by: ipoxygen | last post by:
Hi, I do have 6 identical tables on six different databases (same server). I would like to merge them into one single table for reporting purposes. For the majority of the table it does work...
6
by: Blasting Cap | last post by:
I'm trying to convert a 1.1 app to 2.0, and keep finding one thing after another that either Framework 2.0 or Visual Studio 2005 doesn't like. Now this - In the global.asax, code that has been...
0
by: santhescript01 | last post by:
Unicode to non unicode conversion problem -------------------------------------------------------------------------------- Hi All, I am using C dll in macro which converts Unicode data to...
2
by: Markus Dehmann | last post by:
I have two integers i1 and i2, the second of which is guaranteed to be between 0 and 99, and I encode them into one double: double encoded = (double)i1 + (double)i2 / (double)100; So, for...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.