assign event to var in VB.net

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
 
D

David Anton

Public Overrides Event CollectionChanged As NotifyCollectionChangedEventHandler

'this uses implicit typing in VB - ensure that you have 'Option Infer On':
Dim eh = CollectionChangedEvent
 
D

David Anton

The event declaration was right in your original post - using 'Shadows'
instead of 'Overrides', which (for some reason...) is disallowed in VB.
 
R

Rick

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
 
D

David Anton

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top