0

I have a code snippet in C# but unable to convert to VB.NET. I tried online converters but VS2008 always gives compile errors. Any help is greatly appreciated.

foreach (Binding knownBinding in allKnownBindings)
{
    string errorMessage = ((IDataErrorInfo)this.DataContext)[knownBinding.Path.Path];
    if (errorMessage != null && errorMessage.Length > 0)
    {
        isValid = false;

        // Display the error on any elements bound to the property
        FindBindingsRecursively(
        this.Parent,
        delegate(FrameworkElement element, Binding binding, DependencyProperty dp)
        {
            if (knownBinding.Path.Path == binding.Path.Path)
            {

                BindingExpression expression = element.GetBindingExpression(dp);
                ValidationError error = new ValidationError(
                        new ExceptionValidationRule(), expression, errorMessage, null);
                System.Windows.Controls.Validation.MarkInvalid(expression, error);

                if (_firstInvalidElement == null)
                {
                    _firstInvalidElement = element;
                }
                return;
            }
        });
    }
}

and the VB.Net equivalent I got is:

For Each knownBinding As Binding In allKnownBindings
    Dim errorMessage As String = DirectCast(Me.DataContext, IDataErrorInfo)(knownBinding.Path.Path)
    If errorMessage IsNot Nothing AndAlso errorMessage.Length > 0 Then
        isValid = False

        ''# Display the error on any elements bound to the property
        FindBindingsRecursively(Me.Parent, Function(element As FrameworkElement, binding As Binding, dp As DependencyProperty) Do
            If knownBinding.Path.Path = Binding.Path.Path Then

                Dim expression As BindingExpression = element.GetBindingExpression(dp)
                Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, errorMessage, Nothing)
                System.Windows.Controls.Validation.MarkInvalid(expression, [error])

                If _firstInvalidElement Is Nothing Then
                    _firstInvalidElement = element
                End If

                Return
            End If
        End Function)
    End If
Next
4
  • 2
    What compile errors, and on which lines, do you get? Commented Jul 19, 2010 at 16:54
  • Without further deeper details about the error message you're getting and what line is causing the error, we may not be able to assist you. Commented Jul 19, 2010 at 16:58
  • I get errors all over the above codes after the If Knownbinding.path.path condition check. firstly, its unable to recognize the "Do" statement and as there are two "End Function" statements so everything after the first end fucntion statement its invalid Commented Jul 19, 2010 at 16:59
  • Exact error on "Do" is "Expression expected". Then insdie "Do", "element" is not recognized its giving error " Name element is not declared" similarly "dp" is not declared. Commented Jul 19, 2010 at 17:05

3 Answers 3

3

Try this free service but make sure you provide it with valid C# code.


UPDATE:

I guess the reason the VB.NET compiler is choking is because of the anonymous function passed to FindBindingsRecursively. Try externalizing (deanonymize) this into a separate method:

Sub FindASuitableName(element As FrameworkElement, binding As Binding, dp As DependencyProperty)
    If knownBinding.Path.Path = binding.Path.Path Then
        Dim expression As BindingExpression = element.GetBindingExpression(dp)
        Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, errorMessage, Nothing)
        System.Windows.Controls.Validation.MarkInvalid(expression, [error])

        If _firstInvalidElement Is Nothing Then
            _firstInvalidElement = element
        End If
    End If
End Sub

And then use it directly:

FindBindingsRecursively(Me.Parent, FindASuitableName)
Sign up to request clarification or add additional context in comments.

2 Comments

I already used that, and its same as what I pasted above and vs2008 gives build errors
Thanks Darin! but there is one more problem, I also need knownBinding in the separate method. Actually the method FindBindingsRecursively has two parameters 1) element as dependencyObject and 2) callBackDelegate so I cannot add knownbindings to the separate method.
0

There are varariables that you need to store in a class in order for the delegate method to have access to them.

Public Class BindingWork
...
Private binding As Binding
Private path As String
Private error_msg As String

Public Sub Start()

    Dim errinfo As IDataErrorInfo = CType(Me.DataContext, IDataErrorInfo)
    For Each knownBinding As Binding In allKnownBindings
        error_msg = errinfo(knownBinding.Path.Path)
        If Not String.IsNullOrEmpty(error_msg) Then
            isValid = False
            Me.path = knownBinding.Path.Path
            FindBindingsRecusively(Me.Parent, AddressOf WorkWithBindings)
        End If
    Next

End Sub

Sub WorkWithBindings(ByVal element As FrameworkElement, ByVal binding As Binding, ByVal dp As DependencyProperty)
    If path = binding.Path.Path Then
        Dim expression As BindingExpression = element.GetBindingExpression(dp)
        Dim [error] As New ValidationError(New ExceptionValidationRule(), expression, error_msg, Nothing)
        System.Windows.Controls.Validation.MarkInvalid(expression, [error])
        If _firstInvalidElement Is Nothing Then
            _firstInvalidElement = element
        End If
    End If
End Sub
...
End Class

Notice your delegate handler uses path, error_msg, _firstInvalidElement and 'binding'.

1 Comment

Thanks Jalexiou! I will surely try this way and let you know.
0

Consider trying the Telerik C#/VB converter.

http://converter.telerik.com/

It can convert full files, or snippets.

1 Comment

I tried even that but no change. Its the same code as I have given above :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.