How to fix AddressOf requires a casual conversation with a delegate error

Sorry, this is a combination of C # and VB.Net

I have a C # class with two delegates:

public delegate string GetSettingDelegate(string key); public event GetSettingDelegate GetSettingEvent; public delegate void SetSettingDelegate(string key, string value); public event SetSettingDelegate SetSettingEvent; 

In the VB class, I add handlers to the event:

 AddHandler _gisCtrl.SetSettingEvent, AddressOf SetSetting AddHandler _gisCtrl.GetSettingEvent, AddressOf GetSetting 

When I try to remove handlers:

 RemoveHandler _gisCtrl.SetSettingEvent, AddressOf SetSetting RemoveHandler _gisCtrl.GetSettingEvent, AddressOf GetSetting 

SetSetting is fine, but GetSetting generates a warning:

In this context, the AddressOf expression does not work because the arguments of the AddressOf method require a relaxed conversation with the delagate type of the event.

Here are the methods

 Private Sub SetSetting(ByVal key As String, ByVal value As String) KernMobileBusinessLayer.[Global].Settings.SetValue(key, value) End Sub Private Function GetSetting(ByVal key As String) Return KernMobileBusinessLayer.[Global].Settings.GetString(key) End Function 

Any idea how to fix this and why is it thrown away in the first place? The 2 delegates / events / methods look quite similar, and I don't know why everything is fine, and one warns.

+6
source share
2 answers

perhaps your GetSetting function should fully match the GetSettingDelegate signature:

 Private Function GetSetting(ByVal key As String) as String 
+15
source

your vb code:

 Private Function GetSetting(ByVal key As String) 

not consistent with C # delegate definition:

 public delegate string GetSettingDelegate(string key); 

you must specify the return type in your VB implementation, for example:

 Private Function GetSetting(ByVal key As String) As String 
+6
source

Source: https://habr.com/ru/post/888862/


All Articles