Link to home
Start Free TrialLog in
Avatar of dev_ven
dev_ven

asked on

C# Events and Delegate Conversion to VB Help

I'm trying to convert a bit of C# code into VB and having some difficulties.  It invloves events and delegates.

Here's the following in C#:
public class UniversalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
			return this.OnConverting(value, targetType, parameter, culture);
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			return this.OnConvertingBack(value, targetType, parameter, culture);
		}

		protected object OnConverting(object value, Type targetType, object parameter, CultureInfo culture) {
			UniversalConverterHandler handler = this.Converting;
			if (handler != null) {
				return handler(value, targetType, parameter, culture);
			}
			return value;
		}

		protected object OnConvertingBack(object value, Type targetType, object parameter, CultureInfo culture) {
			UniversalConverterHandler handler = this.ConvertingBack;
			if (handler != null) {
				return handler(value, targetType, parameter, culture);
			}
			return value;
		}

		public event UniversalConverterHandler Converting;

		public event UniversalConverterHandler ConvertingBack;

	}

	public delegate object UniversalConverterHandler(object value, Type targetType, object parameter, CultureInfo culture);

Open in new window


Thanks in advance,
-Mike
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

I swear I attached the code:

Public Class UniversalConverter
      Implements IValueConverter
      Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Return Me.OnConverting(value, targetType, parameter, culture)
      End Function

      Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Return Me.OnConvertingBack(value, targetType, parameter, culture)
      End Function

      Protected Function OnConverting(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Dim handler As UniversalConverterHandler = Me.Converting
            If handler IsNot Nothing Then
                  Return handler(value, targetType, parameter, culture)
            End If
            Return value
      End Function

      Protected Function OnConvertingBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
            Dim handler As UniversalConverterHandler = Me.ConvertingBack
            If handler IsNot Nothing Then
                  Return handler(value, targetType, parameter, culture)
            End If
            Return value
      End Function

      Public Event Converting As UniversalConverterHandler

      Public Event ConvertingBack As UniversalConverterHandler

End Class

Public Delegate Function UniversalConverterHandler(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object
Avatar of dev_ven
dev_ven

ASKER

I've done that, but there are a few issues that I need assitance with.

1.  VB doesn't like Delegate Functions
Public Delegate Function UniversalConverterHandler(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object

2.  VB wants you to Raise the event.
Dim handler As UniversalConverterHandler = Me.Converting

I've been trying all sorts of ways to get this to work.  I've come close but I never get anything returned.
Take 2.

Public Class UniversalConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        Return Me.OnConverting(value, targetType, parameter, culture)
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Return Me.OnConvertingBack(value, targetType, parameter, culture)
    End Function

    Protected Function OnConverting(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
        If Converting IsNot Nothing Then
            Return Converting.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function

    Protected Function OnConvertingBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
        If ConvertingBack IsNot Nothing Then
            Return ConvertingBack.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function

    Public Converting As UniversalConverterHandler
    Public ConvertingBack As UniversalConverterHandler
End Class

Public Delegate Function UniversalConverterHandler(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
As a slight modification, I'd collapse some of those methods:

Public Class UniversalConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        If Converting IsNot Nothing Then
            Return Converting.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        If ConvertingBack IsNot Nothing Then
            Return ConvertingBack.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function

    Public Converting As UniversalConverterHandler
    Public ConvertingBack As UniversalConverterHandler
End Class

Public Delegate Function UniversalConverterHandler(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
Avatar of dev_ven

ASKER

That seems like it could work, but I need to call an event.  To give you an idea what I'm looking at, here's the URL.

How would I go about handling what was events?

Dim uc1 As UniversalConverter = TryCast(Me.Resources("ageConverter"), UniversalConverter)
        AddHandler uc1.Converting, AddressOf ConvertAge

        Dim uc2 As UniversalConverter = TryCast(Me.Resources("nameConverter"), UniversalConverter)
        AddHandler uc1.Converting, AddressOf ConvertName

Open in new window

I don't think you can follow the exact same pattern in VB.NET.

This is the closest I can get:

Public Class UniversalConverter
    Implements IValueConverter

    Private _convertDelegate As UniversalConverterDelegate
    Private _convertBackDelegate As UniversalConverterDelegate

    Public Sub New(ByVal convertDelegate As UniversalConverterDelegate, ByVal convertBackDelegate As UniversalConverterDelegate)
        _convertDelegate = convertDelegate
        _convertBackDelegate = convertBackDelegate
    End Sub

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        If _convertDelegate IsNot Nothing Then
            RaiseEvent Converting(value, targetType, parameter, culture)
            Return _convertDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If

        Return value
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        If _convertBackDelegate IsNot Nothing Then
            RaiseEvent ConvertingBack(value, targetType, parameter, culture)
            Return _convertBackDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function

    Public Event Converting(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo)
    Public Event ConvertingBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo)
End Class

Public Delegate Function UniversalConverterDelegate(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object

As you've notived, I'm not a WPF/Silverlight guru. However, this link (http://visualbasic.about.com/od/learnvbnet/a/wpfmkupex2_2.htm) indicates that you don't need the events at all for VB.NET implementations.

You should be able to just use this:
Public Class UniversalConverter
    Implements IValueConverter

    Private _convertDelegate As UniversalConverterDelegate
    Private _convertBackDelegate As UniversalConverterDelegate

    Public Sub New(ByVal convertDelegate As UniversalConverterDelegate, ByVal convertBackDelegate As UniversalConverterDelegate)
        _convertDelegate = convertDelegate
        _convertBackDelegate = convertBackDelegate
    End Sub

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        If _convertDelegate IsNot Nothing Then
            Return _convertDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If

        Return value
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        If _convertBackDelegate IsNot Nothing Then
            Return _convertBackDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function
End Class

Public Delegate Function UniversalConverterDelegate(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
Avatar of dev_ven

ASKER

I really appreciate the effort, but I'm getting a whole new error in my XAML.

This error occurs when you attempt to instantiate a type by using element syntax, but the type does not have a public constructor, a type converter, or is not public.
You'll need to set the delgates, much like you were setting event handlers.

Public Class UniversalConverter
    Implements IValueConverter

    Public ConvertDelegate As UniversalConverterDelegate
    Public ConvertBackDelegate As UniversalConverterDelegate

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
        If ConvertDelegate IsNot Nothing Then
            Return ConvertDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If

        Return value
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        If ConvertBackDelegate IsNot Nothing Then
            Return ConvertBackDelegate.DynamicInvoke(value, targetType, parameter, culture)
        End If
        Return value
    End Function
End Class

Public Delegate Function UniversalConverterDelegate(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object
Avatar of dev_ven

ASKER

Sorry, but I'm not following that first statement.

The error is gone.  But I'm left with no way of triggering the event to begin converting.

ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dev_ven

ASKER

Wow.  I'm so sorry you had to walk me through that.

You don't know how much I really appreciate this.

Thank you!
No worries. Glad it works! :)