Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error in code converted to VB.NET from C# [duplicate]

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)
like image 725
Nikhil Agrawal Avatar asked Mar 14 '26 12:03

Nikhil Agrawal


2 Answers

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)
like image 95
Dennis Avatar answered Mar 16 '26 02:03

Dennis


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))
like image 26
Burak SARICA Avatar answered Mar 16 '26 02:03

Burak SARICA