assign event to var in VB.net

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

I am trying to convert this C# code to VB and do not know how to assign the
event to a variable in VB.Net 2008. (see C# 4th line "var eh =
CollectionChanged")

Can anyone tell me if this is possible in VB?

Rick


C#
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void
OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
var eh = CollectionChanged;
if (eh != null)
{
Dispatcher dispatcher = (from NotifyCollectionChangedEventHandler
nh in eh.GetInvocationList()
let dpo = nh.Target as DispatcherObject
where dpo != null
select dpo.Dispatcher).FirstOrDefault();

if (dispatcher != null && dispatcher.CheckAccess() == false)
{
dispatcher.Invoke(DispatcherPriority.DataBind, (Action)(() =>
OnCollectionChanged(e)));
}
else
{
foreach (NotifyCollectionChangedEventHandler nh in
eh.GetInvocationList())
nh.Invoke(this, e);
}
}
}


VB

Public Shadows Event CollectionChanged As
Specialized.NotifyCollectionChangedEventHandler

Protected Overloads Overrides Sub OnCollectionChanged(ByVal e As
Specialized.NotifyCollectionChangedEventArgs)

Dim eh As Specialized.NotifyCollectionChangedEventHandler =
CollectionChanged()

If eh IsNot Nothing Then
Dim dispatcher As Threading.Dispatcher = (From nh In
eh.GetInvocationList() _
Let dpo = TryCast(nh.Target, DispatcherObject) _
Where dpo IsNot Nothing _
Select dpo.Dispatcher).FirstOrDefault()

If dispatcher IsNot Nothing AndAlso dispatcher.CheckAccess()
= False Then
dispatcher.Invoke(Threading.DispatcherPriority.DataBind,
DirectCast((Function() OnCollectionChanged(e)), Action))
Else
For Each nh As
Specialized.NotifyCollectionChangedEventHandler In eh.GetInvocationList()
nh.Invoke(Me, e)
Next
End If
End If
End Sub
 
Public Overrides Event CollectionChanged As NotifyCollectionChangedEventHandler

'this uses implicit typing in VB - ensure that you have 'Option Infer On':
Dim eh = CollectionChangedEvent
 
The event declaration was right in your original post - using 'Shadows'
instead of 'Overrides', which (for some reason...) is disallowed in VB.
 
Thanks both,

I don't know if it works yet, but at least I got further along.

Now I need to know how to refer to an anonymous method in VB. The compile
won't accept this:

disp.Invoke(Threading.DispatcherPriority.DataBind, DirectCast((Function()
OnCollectionChanged(e)), Action))

in C# it is this:

dispatcher.Invoke(DispatcherPriority.DataBind, (Action)(() =>
OnCollectionChanged(e)));

Rick
 
Try:
disp.Invoke(Threading.DispatcherPriority.DataBind, CType(Function()
OnCollectionChanged(e), Action))
 
Back
Top