2

Possible Duplicate: Method group in VB.NET?

While reading an answer I got this code:

public static class Helper
{
    public static bool GetAutoScroll(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoScrollProperty);
    }

    public static void SetAutoScroll(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoScrollProperty, value);
    }

    public static readonly DependencyProperty AutoScrollProperty =
        DependencyProperty.RegisterAttached("AutoScroll", typeof(bool),
        typeof(Helper),
        new PropertyMetadata(false, AutoScrollPropertyChanged));

    private static void AutoScrollPropertyChanged(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        var scrollViewer = d as ScrollViewer;

        if (scrollViewer != null && (bool)e.NewValue)
        {
            scrollViewer.ScrollToBottom();
        }
    }
}

Since I work in VB.NET, so I converted it and got:

Public NotInheritable Class Helper

    Private Sub New()
    End Sub

    Public Shared Function GetAutoScroll(ByVal obj As DependencyObject)
    As Boolean
        Return CBool(obj.GetValue(AutoScrollProperty))
    End Function

    Public Shared Sub SetAutoScroll(ByVal obj As DependencyObject,
    ByVal value As Boolean)
        obj.SetValue(AutoScrollProperty, value)
    End Sub

    Public Shared ReadOnly AutoScrollProperty As DependencyProperty =
        DependencyProperty.RegisterAttached("AutoScroll", GetType(Boolean),
        GetType(Helper),
        New PropertyMetadata(False, AutoScrollPropertyChanged)) // Error Here

    Private Shared Sub AutoScrollPropertyChanged(ByVal d As
    System.Windows.DependencyObject, ByVal e As
    System.Windows.DependencyPropertyChangedEventArgs)
        Dim scrollViewer = TryCast(d, ScrollViewer)

        If scrollViewer IsNot Nothing AndAlso CBool(e.NewValue) Then
            scrollViewer.ScrollToBottom()
        End If
    End Sub

End Class

But the C# code compiles and works fine, but in VB.NET the code gives an error (marked in code) saying:

Argument not specified for parameter 'e' of 'Private Shared Sub AutoScrollPropertyChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)'

Eenter image description here

What am I missing? The PropertyChangedCallback delegate is exactly the way it is defined in Object Browser:

Public Delegate Sub PropertyChangedCallback(
    ByVal d As System.Windows.DependencyObject, ByVal e As
    System.Windows.DependencyPropertyChangedEventArgs)
Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • See also the other questions about method groups in VB.Net [Method groups in VB.Net](http://stackoverflow.com/questions/12803897/method-group-in-vb-net) and [*vague title* actually about method groups](http://stackoverflow.com/questions/8047249/how-to-do-this-in-vb-net) – MarkJ Oct 11 '12 at 08:47

2 Answers2

8

C# has a language feature, that can convert method groups to delegate type. So, instead of:

private void Foo() {}
private void Bar(Action arg) {}

Bar(new Action(Foo));

you can write:

Bar(Foo);

I'm not a VB guy, but I suspect, that VB .NET hasn't such feature. Looks like you need AddressOf operator:

New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged)
Dennis
  • 37,026
  • 10
  • 82
  • 150
4

I did not compile it, but I think you should refer AutoScrollPropertyChanged with the AddressOf operator:

Public Shared ReadOnly AutoScrollProperty As DependencyProperty =
        DependencyProperty.RegisterAttached("AutoScroll", GetType(Boolean),
        GetType(Helper),
        New PropertyMetadata(False, AddressOf AutoScrollPropertyChanged))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Burak SARICA
  • 721
  • 1
  • 7
  • 27