1

i converted a c# class to vb net, but there are some events that couldnt find out these are the delegates declarations:

    Public Delegate Sub IndentChangedEventHandler(NewValue As Integer)
    Public Delegate Sub MultiIndentChangedEventHandler(LeftIndent As Integer, HangIndent As Integer)
    Public Delegate Sub MarginChangedEventHandler(NewValue As Integer)
    Public Delegate Sub TabChangedEventHandler(args As TabEventArgs)

    Public Event LeftHangingIndentChanging As IndentChangedEventHandler
    Public Event LeftIndentChanging As IndentChangedEventHandler
    Public Event RightIndentChanging As IndentChangedEventHandler
    Public Event BothLeftIndentsChanged As MultiIndentChangedEventHandler

    Public Event LeftMarginChanging As MarginChangedEventHandler
    Public Event RightMarginChanging As MarginChangedEventHandler

    Public Event TabAdded As TabChangedEventHandler
    Public Event TabRemoved As TabChangedEventHandler
    Public Event TabChanged As TabChangedEventHandler

'this is the function converted on vb net

Private Sub AddTab(pos As Single)
            Dim rect As New RectangleF(pos, 10.0F, 8.0F, 8.0F)
            tabs.Add(rect)
            If TabAdded IsNot Nothing Then
                TabAdded.Invoke(CreateTabArgs(pos))
            End If
        End Sub

the sentence on c# was

if (TabAdded != null)
                TabAdded.Invoke(CreateTabArgs(pos));

what should it be the correct way to call the delegate?

aleroot
  • 71,077
  • 30
  • 176
  • 213
CJ Siete
  • 23
  • 3

2 Answers2

0

I think you should use Address Of. Conversion of method to delegate is implicitly in C#. In VB.Net its explicitly and we use Address Of. Converters forget this thing.

For More read here

Strange error in code converted to VB.NET from C#

Method group in VB.NET?

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0
if (TabAdded != null)
            TabAdded.Invoke(CreateTabArgs(pos));

Events have three accessors in vb.net: add, remove and raise. C# doesn't support the raise accessor so you have to explicitly test for null. That is not necessary in vb.net, and not allowed, just use the RaiseEvent statement directly without testing for Nothing:

Private Sub AddTab(ByVal pos As Single)
    Dim rect As New RectangleF(pos, 10.0F, 8.0F, 8.0F)
    tabs.Add(rect)
    RaiseEvent TabAdded(CreateTabArgs(pos))
End Sub
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536