Home > Back-end >  BC31143 Error when using a delegate in a form load
BC31143 Error when using a delegate in a form load

Time:01-16

I have a form with two types of datas:

  • current datas which have to be displayed when the form is shown
  • historical datas which may be displayed if the user click on a dedicated button. Both types came from a database.

As historical datas might take more time to get, i'd like to get them after the form is shown.

The form is opened through this:

Public Sub DisplayUgc(_strIdUgcToDisplay As String, _strPlatformUgcToDisplay As String)
   Dim frmUgcNotice as New frmNoticeUgc
   If IsMyFormOpened(frmUgcNotice) = False Then
      frmUgcNotice.strIdUgcToDisplay = _strIdUgcToDisplay
      frmUgcNotice.strPlatformUgcToDisplay = _strPlatformUgcToDisplay
      frmUgcNotice.Show()
   Else
      MsgBox(strMsgNoticeAlreadyOpened, MsgBoxStyle.Exclamation, strMsgBoxGeneralErrorTitle)
      Exit Sub
   End If

'[...Then, some code where every other controls in the form is set...]

So, to get what i'd like and following this post, i did this:

Public Class frmNoticeUgc
    Inherits Form

    Public Delegate Sub DoWorkDelegate()

    Public Sub New()
        InitializeComponent()
    End Sub    
    Friend tblHistoricalDatas As New DataTable  ' Historical Datas
    Friend WithEvents strIdUgcToDisplay As String
    Friend WithEvents strPlatformUgcToDisplay As String
    Private Sub frmNoticeUgc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        
        InitUgcCombobox()
        InitUgcDatagrid()

        Select Case Me.btnValidation.Text
            Case "Ajouter"
                SetFormControls("add")
            Case "Modifier"
                Me.tblHistoricalDatas  = BeginInvoke(New DoWorkDelegate(AddressOf GetHistoricalDatas(Me.strIdUgcToDisplay, Me.strPlatforme))
                SetFormControls("modify")
        End Select
        SetFormColor(Me)

    End Sub

With GetHistoricalDatas like:

Public Function GetHistoricalDatas(_strUserId As String, _strPlatforme As String) As DataTable
      Dim tblGetAllVersions As Datatable
      ' [...] Some code to get the historical datas

       Return tblGetAllVersions
        
End Function

Issue: i have a BC31143, Method 'Public Function GetHistoricalDatas(_strUserId As String, _strPlatforme As String) As DataTable' does not have a signature compatible with delegate 'frmNoticeUgc.DoWorkDelegate'. I don't understand what causes this issue condiering that tblHistoricalDatas is a datatable and GetHistoricalDatas returns a datatable

What did i miss?

Precision: GetHistoricalDatas is not in the form code, this function is located elsewhere in the project. But, as it's a public function, i'm assuming (maybe i'm wrong), it won't be an issue.

CodePudding user response:

You cannot invoke a method using a delegate with a different signature. Your method is a Function with two String parameters and returns a DataTable while your delegate is a Sub with no parameters. You should declare your delegate with the same signature as the method you intend to invoke:

Public Delegate Function DoWorkDelegate(p1 As String, p2 As String) As DataTable

Note that the parameter names don't actually matter but you should give them appropriate names, like everything else. If the delegate will only be used to invoke one method, use the same parameter names as the method.

That said, there's really little point declaring your own delegates these days. To invoke a Sub, use an appropriate Action delegate and, to invoke a Function, use an appropriate Func delegate. Both can have up to 16 parameters.

Note that this addresses the specific question you asked but there are still issues with your code and this is definitely not the way I would go about it.

  • Related