C# to VB.NET conversion issue

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
I'm converting the Prism Navigation Framework from C# to VB.NET and have 1 issue with an EventHandler that's giving me headaches.

The original Interface:

VB.NET:
using System;

namespace PrismNavigationFramework.Infrastructure
{
    public interface IPrismModuleLoadingService
    {
        bool ModuleNeedsToBeLoaded(string moduleName);
        void LoadModule(string moduleUrlFriendlyName);
        EventHandler<ModuleDownloadStateChangedEventArgs> ModuleDownloadStateChanged { get; set; }
    }
}

The converted interface:

VB.NET:
Namespace Infrastructure
    Public Interface IPrismModuleLoadingService
        Function ModuleNeedsToBeLoaded(ByVal moduleName As String) As Boolean
        Sub LoadModule(ByVal moduleUrlFriendlyName As String)
        Property ModuleDownloadStateChanged() As EventHandler(Of ModuleDownloadStateChangedEventArgs)
    End Interface
End Namespace

Then here's the original implementation in PrismModuleLoadingService.cs:

VB.NET:
        public EventHandler<ModuleDownloadStateChangedEventArgs> ModuleDownloadStateChanged
        {
            get;
            set;
        }

And the conversion:

VB.NET:
        Public Property ModuleDownloadStateChanged() As EventHandler(Of ModuleDownloadStateChangedEventArgs) _
            Implements IPrismModuleLoadingService.ModuleDownloadStateChanged

These both compile fine.

The problem is when I convert the file PrismViewLoadingService.cs

Here's the original constructor:

VB.NET:
        public PrismViewLoadingService(
            IPrismNavigationConfiguration prismNavigationConfiguration,
            IRegionManager regionManager, 
            IViewCreationService viewCreationService, 
            IViewToUrlMappingRegistrationService viewToUrlMappingService, 
            IViewNavigationContextInformationParsingService viewNavigationInformationParsingService, 
            IPrismModuleLoadingService moduleLoadingService,
            IModuleNamesRegistrationService moduleNamesRegistrationService,
            IViewInitializationService viewInitializationService,
            IKeyValuePairsParsingService keyValuePairsParsingService
            )
        {
            _prismNavigationConfiguration = prismNavigationConfiguration;
            _regionManager = regionManager;
            _viewCreationService = viewCreationService;
            _viewToUrlMappingService = viewToUrlMappingService;
            _viewNavigationInformationParsingService = viewNavigationInformationParsingService;
            _moduleLoadingService = moduleLoadingService;
            _moduleNamesRegistrationService = moduleNamesRegistrationService;
            _viewInitializationService = viewInitializationService;
            _keyValuePairsParsingService = keyValuePairsParsingService;

            this._moduleLoadingService.ModuleDownloadStateChanged += this.ModuleDownloadStateChanged;
        }

And the conversion:

VB.NET:
        Public Sub New(ByVal prismNavigationConfiguration As IPrismNavigationConfiguration,
                       ByVal regionManager As IRegionManager,
                       ByVal viewCreationService As IViewCreationService,
                       ByVal viewToUrlMappingService As IViewToUrlMappingRegistrationService,
                       ByVal viewNavigationInformationParsingService As IViewNavigationContextInformationParsingService,
                       ByVal moduleLoadingService As IPrismModuleLoadingService,
                       ByVal moduleNamesRegistrationService As IModuleNamesRegistrationService,
                       ByVal viewInitializationService As IViewInitializationService,
                       ByVal keyValuePairsParsingService As IKeyValuePairsParsingService)

            _prismNavigationConfiguration = prismNavigationConfiguration
            _regionManager = regionManager
            _viewCreationService = viewCreationService
            _viewToUrlMappingService = viewToUrlMappingService
            _viewNavigationInformationParsingService = viewNavigationInformationParsingService
            _moduleLoadingService = moduleLoadingService
            _moduleNamesRegistrationService = moduleNamesRegistrationService
            _viewInitializationService = viewInitializationService
            _keyValuePairsParsingService = keyValuePairsParsingService

            AddHandler _moduleLoadingService.ModuleDownloadStateChanged, AddressOf ModuleDownloadStateChanged
        End Sub

The problem is with the AddHandler line on the conversion. I'm getting the message that ModuleDownloadStateChanged is not an event of Infrastructure.IPrismModuleLoadingService. Well, yeah...it's an EventHandler.

I have the feeling the solution looks something like this but it's been a long week and the answer is eluding me:

VB.NET:
            Dim handler = _moduleLoadingService.ModuleDownloadStateChanged
            If handler IsNot Nothing Then
                '...
            End If
 
Long day Friday...thanks for the push JohnH.

VB.NET:
            [Delegate].Combine(_moduleLoadingService.ModuleDownloadStateChanged,
                               Sub(sender, e)
                                   ModuleDownloadStateChanged(sender, e)
                               End Sub)
 
Though it could of course be possible that it should be an event rather than a delegate type property... :)
 
Though it could of course be possible that it should be an event rather than a delegate type property... :)

That was my thought and source of my confusion (That and I'd already converted 30+ classes/interfaces that day and my brain went into hibernate mode). Unless I start seeing errors thrown on Lambda__1 I'll probably leave it be.

Thanks again for helping me step back and read what I was actually working with.
 
Back
Top