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#:
Thanks in advance,
-Mike
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);
Thanks in advance,
-Mike
Try this. (courtesy of http://www.developerfusion.com/tools/convert/csharp-to-vb/)
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
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,
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(
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.
1. VB doesn't like Delegate Functions
Public Delegate Function UniversalConverterHandler(
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.ConvertBac k
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(v alue, 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.DynamicInvo ke(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
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.ConvertBac
Return Me.OnConvertingBack(value,
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(v
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.DynamicInvo
End If
Return value
End Function
Public Converting As UniversalConverterHandler
Public ConvertingBack As UniversalConverterHandler
End Class
Public Delegate Function UniversalConverterHandler(
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(v alue, 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.ConvertBac k
If ConvertingBack IsNot Nothing Then
Return ConvertingBack.DynamicInvo ke(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
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(v
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.ConvertBac
If ConvertingBack IsNot Nothing Then
Return ConvertingBack.DynamicInvo
End If
Return value
End Function
Public Converting As UniversalConverterHandler
Public ConvertingBack As UniversalConverterHandler
End Class
Public Delegate Function UniversalConverterHandler(
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?
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
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.DynamicIn voke(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.ConvertBac k
If _convertBackDelegate IsNot Nothing Then
RaiseEvent ConvertingBack(value, targetType, parameter, culture)
Return _convertBackDelegate.Dynam icInvoke(v alue, 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
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
_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.DynamicIn
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.ConvertBac
If _convertBackDelegate IsNot Nothing Then
RaiseEvent ConvertingBack(value, targetType, parameter, culture)
Return _convertBackDelegate.Dynam
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
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.DynamicIn voke(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.ConvertBac k
If _convertBackDelegate IsNot Nothing Then
Return _convertBackDelegate.Dynam icInvoke(v alue, 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
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
_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.DynamicIn
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.ConvertBac
If _convertBackDelegate IsNot Nothing Then
Return _convertBackDelegate.Dynam
End If
Return value
End Function
End Class
Public Delegate Function UniversalConverterDelegate
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.
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.DynamicInv oke(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.ConvertBac k
If ConvertBackDelegate IsNot Nothing Then
Return ConvertBackDelegate.Dynami cInvoke(va lue, 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
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.DynamicInv
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.ConvertBac
If ConvertBackDelegate IsNot Nothing Then
Return ConvertBackDelegate.Dynami
End If
Return value
End Function
End Class
Public Delegate Function UniversalConverterDelegate
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.
The error is gone. But I'm left with no way of triggering the event to begin converting.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
You don't know how much I really appreciate this.
Thank you!
No worries. Glad it works! :)