473,322 Members | 1,473 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,322 software developers and data experts.

Confused when converting code from C# to VB...

Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.

I'm trying to convert the following C# code...

public event EventHandler<EnterKeyEventArgsEnterKeyEvent;

private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}

into VB, and so far I have...

Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)

If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If

End Sub

There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...

"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"

I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!

Karl

Aug 13 '07 #1
10 5619
"Karl Rhodes" <go**********@cortexa.co.ukschrieb:
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
Simply remove the check for 'Nothing'. 'RaiseEvent' will only raise the
event if delegates are attached, which is unfortunately not done in C#.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 13 '07 #2
On 13 Aug, 22:43, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.atwrote:
"Karl Rhodes" <googlegro...@cortexa.co.ukschrieb:
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If

Simply remove the check for 'Nothing'. 'RaiseEvent' will only raise the
event if delegates are attached, which is unfortunately not done in C#.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Hi Herfried,

I'm afraid it hasn't made any difference. I changed the line to read
both "If EnterKeyEvent Then" and "If (EnterKeyEvent) Then" and I still
have the same error...

Did you mean for me to remove the if statement altogether?

Karl

Aug 13 '07 #3
"Karl Rhodes" <go**********@cortexa.co.ukschrieb:
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If

Simply remove the check for 'Nothing'. 'RaiseEvent' will only raise the
event if delegates are attached, which is unfortunately not done in C#.

I'm afraid it hasn't made any difference. I changed the line to read
both "If EnterKeyEvent Then" and "If (EnterKeyEvent) Then" and I still
have the same error...

Did you mean for me to remove the if statement altogether?
Yes, it's not required at all in VB.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Aug 13 '07 #4
Herfried is right - you do not need to do this sort of check in VB.

But... sometimes you may want to check if handlers are wired to the event (I
personally have never required this).
In that case, you could do the following, using VB's very obscure hidden
<event>Event variable:
If Not EnterKeyEventEvent Is Nothing Then

Note the extra "Event" - this refers to the hidden variable.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
"Karl Rhodes" wrote:
Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.

I'm trying to convert the following C# code...

public event EventHandler<EnterKeyEventArgsEnterKeyEvent;

private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}

into VB, and so far I have...

Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)

If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If

End Sub

There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...

"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"

I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!

Karl

Aug 14 '07 #5
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Karl Rhodes" <go**********@cortexa.co.ukschrieb:
> If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If

Yes, it's not required at all in VB.
I'd be inclinded to include it anyway for efficiency.

Michael
Aug 14 '07 #6
Karl Rhodes wrote:
Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.

I'm trying to convert the following C# code...

public event EventHandler<EnterKeyEventArgsEnterKeyEvent;

private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}

into VB, and so far I have...

Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)

If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If

End Sub

There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...

"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"

I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!

Karl
Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
' handle enter code cancellation
Dim cancel As Boolean = False
If EnterKeyEvent IsNot Nothing Then
Dim args As New EnterKeyEventArgs()
EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub
Aug 14 '07 #7
nil
On Aug 14, 12:13 am, Mick Walker <materiali...@privacy.netwrote:
Karl Rhodes wrote:
Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.
I'm trying to convert the following C# code...
public event EventHandler<EnterKeyEventArgsEnterKeyEvent;
private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}
into VB, and so far I have...
Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)
Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub
There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...
"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"
I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!
Karl

Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
' handle enter code cancellation
Dim cancel As Boolean = False
If EnterKeyEvent IsNot Nothing Then
Dim args As New EnterKeyEventArgs()
EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub

you just logon to
http://www.eggheadcafe.com/articles/...converter.aspx
and convert your whole c# code to vb code...
i hope this will help you.....

Regards,
Nilesh
"work expands to fill the time available........"

Aug 14 '07 #8
No - that doesn't compile either.
You either have to forget the null check (as Herfried indicated - it's not
required) or you have to use the hidden VB <event>Event variable.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
"Mick Walker" wrote:
Karl Rhodes wrote:
Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.

I'm trying to convert the following C# code...

public event EventHandler<EnterKeyEventArgsEnterKeyEvent;

private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}

into VB, and so far I have...

Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)

If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If

End Sub

There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...

"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"

I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!

Karl
Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
' handle enter code cancellation
Dim cancel As Boolean = False
If EnterKeyEvent IsNot Nothing Then
Dim args As New EnterKeyEventArgs()
EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub
Aug 14 '07 #9
And the fact that this converter fails (really fails) for this sample doesn't
deter you from recommending it?
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
"nil" wrote:
On Aug 14, 12:13 am, Mick Walker <materiali...@privacy.netwrote:
Karl Rhodes wrote:
Hi all, I'm in the middle of converting some C# code to VB and I'm
almost done, but this last little bit is confusing me.
I'm trying to convert the following C# code...
public event EventHandler<EnterKeyEventArgsEnterKeyEvent;
private void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 13 && !e.ShiftKeyPressed)
{
// handle enter code cancellation
bool cancel = false;
if (EnterKeyEvent != null)
{
EnterKeyEventArgs args = new EnterKeyEventArgs();
EnterKeyEvent(this, args);
cancel = args.Cancel;
}
e.ReturnValue = !cancel;
}
}
into VB, and so far I have...
Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)
Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
Dim cancel As Boolean = False
If Not IsNothing(EnterKeyEvent) Then 'PROBLEM LINE
Dim args As New EnterKeyEventArgs()
RaiseEvent EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub
There is a line in the VB section above with a comment "PROBLEM LINE"
at the end. Visual Studio gives me the following error...
"
'Public Event EnterKeyEvent(sender as Object, e as EnterKeyEventArgs)'
is an event, and cannot be called directly. Use a 'RaiseEvent'
statement to raise an event
"
I thought this was checking for an event and not trying to raise one?
If anyone know's how I should do this I would be most grateful!
Karl
Public Event EnterKeyEvent As EventHandler(Of EnterKeyEventArgs)

Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As
HtmlElementEventArgs)
If e.KeyPressedCode = 13 AndAlso Not e.ShiftKeyPressed Then
' handle enter code cancellation
Dim cancel As Boolean = False
If EnterKeyEvent IsNot Nothing Then
Dim args As New EnterKeyEventArgs()
EnterKeyEvent(Me, args)
cancel = args.Cancel
End If
e.ReturnValue = Not cancel
End If
End Sub


you just logon to
http://www.eggheadcafe.com/articles/...converter.aspx
and convert your whole c# code to vb code...
i hope this will help you.....

Regards,
Nilesh
"work expands to fill the time available........"

Aug 14 '07 #10
"David Anton" <Da********@discussions.microsoft.comschrieb:
And the fact that this converter fails (really fails) for this sample
doesn't
deter you from recommending it?
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Well, I can really recommend the Instant converters because of the high
quality of the conversion results :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 14 '07 #11

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

Similar topics

6
by: Simon | last post by:
Hi all I am writing a small app that uses real numbers all over the place for calculations. But when it comes to displaying values it is better to only display an it, (especially when it...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
9
by: CsharpGuy | last post by:
Ok, here is my scenario, I have an asp web app that i'm converting to .net. The app reads files and loads the data into SQL db, now, in my file it has numbers like, 125.25, 3363.33, 69.00, and when...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
2
by: Lee J | last post by:
Gang, I am self taught in C and fairly proficient for last 20 years. Don't program much, just little utilities for work. Been wanting to create windows forms and found MS VC++ Express. ...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.