• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

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
0
dev_ven
Asked:
dev_ven
  • 9
  • 5
1 Solution
 
Jeff CertainCommented:
0
 
Jeff CertainCommented:
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
0
 
dev_venAuthor Commented:
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.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
Jeff CertainCommented:
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
0
 
Jeff CertainCommented:
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
0
 
dev_venAuthor Commented:
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

0
 
Jeff CertainCommented:
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

0
 
Jeff CertainCommented:
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
0
 
dev_venAuthor Commented:
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.
0
 
Jeff CertainCommented:
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
0
 
dev_venAuthor Commented:
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.

0
 
Jeff CertainCommented:
Instead of:
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

You'll need to have:
Dim uc1 As UniversalConverter = TryCast(Me.Resources("ageConverter"), UniversalConverter)
uc1.ConvertDelegate = AddressOf ConvertAge

Dim uc2 As UniversalConverter = TryCast(Me.Resources("nameConverter"), UniversalConverter)
uc2.ConvertDelegate = AddressOf ConvertName
0
 
dev_venAuthor Commented:
Wow.  I'm so sorry you had to walk me through that.

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

Thank you!
0
 
Jeff CertainCommented:
No worries. Glad it works! :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now