I have a legacy solution which has some C# projects (newest ones) and some VB projects (oldest ones). I know it is not a good practice, but it is what I have now, so I have to deal with it.
In a C# project I have some code like this:
public class MyObjectClass
{
... more code here ...
public delegate void SelectedItemChange(IMyInterface oldItem, IMyInterface newItem);
public virtual SelectedItemChange SelectedItemChanged { get; set; }
}
And this works like a charm when I use it from another C# project this way:
public class MySecondaryClass
{
... more code here ...
MyObjectClass MyObject = new();
MyObject.SelectedItemChanged = (sender, args) => { DoSomething(); };
}
So far, so good. The problem is when I have to use the same delegate from a VB.NET project. I have tried this:
Public Class MySecondaryClassVB
... more code here ...
Dim MyObject as MyObjectClass = New MyObjectClass()
AddHandler MyObject.SelectedItemChanged, Sub(sender As Object, args As EventArgs)
DoSomething()
End Sub
End Class
But SelectedItemChanged
appears in Visual Studio underlined in red with the next error:
'SelectedItemChanged' is not an event of 'MyObjectClass'
I am not able to understand why it works in C# and not in VB.NET, so I guess my AddHandler
syntax is not correct. What is the right syntax for this? What am I doing wrong?
Thank you
UPDATE: Maybe it is useful to understand the problem. The real MyObjectClass is there I emit the event:
public class MyObjectClass
{
... more code here ...
private IMyInterface _selectedItem;
public IMyInterface SelectedItem
{
get => _selectedItem;
set
{
IMyInterface oldSelectedItem = _selectedItem;
_selectedItem = value;
SelectedItemChanged?.Invoke(oldSelectedItem, _selectedItem);
SetProperty(ref _selectedItem, value);
}
}
public delegate void SelectedItemChange(IMyInterface oldItem, IMyInterface newItem);
public virtual SelectedItemChange SelectedItemChanged { get; set; }
}
Then I want to subscribe to that event from a C# project (and I do it with no problem), or from a VB.NET project (and I am not able to).
CodePudding user response:
It´s because SelectedItemChange is not an event. You need to declare it as event
public event SelectedItemChange SelectedItemChange_Event;
Your AddHanler Syntax in VB is correct
CodePudding user response:
Try this:
Public Event SelectedItemChange_Event As SelectedItemChange