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

K

Karl Rhodes

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<EnterKeyEventArgs> EnterKeyEvent;

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
 
H

Herfried K. Wagner [MVP]

Karl Rhodes said:
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#.
 
K

Karl Rhodes

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

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
 
H

Herfried K. Wagner [MVP]

Karl Rhodes said:
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.
 
G

Guest

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
 
M

Mick Walker

Karl said:
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<EnterKeyEventArgs> EnterKeyEvent;

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
 
N

nil

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/cstovbweb/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........"
 
G

Guest

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
 
H

Herfried K. Wagner [MVP]

David Anton said:
And the fact that this converter fails (really fails) for this sample
doesn't
deter you from recommending it?

Well, I can really recommend the Instant converters because of the high
quality of the conversion results :).
 

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

Top