How to pass Microsoft.Exchange.WebService.Data.ExchangeVersion Objects to and from function(s) RRS feed

  • Question

  • Dear All, 

    I am currently struggling with Microsoft.Exchange.WebServices.Data in VB.NET. I was told to post my question in this section of the forum. I am writing an application helping users to download specfic documents from their mail servers. The documents are then processed and data mined automatically. The problem is that the application might have several users all of which might have different ExchangeVersions installed. My idea was to provide users with a list (backed up by a col in a database) from which they can select their Exchange-Server-Version. The selected value is passed to a function creating the actual connection. The function is designed as follows:

     Public Shared Function ConnectToExchangeServer(userName As String, _
                                                        ByVal emailAdr As String, _
                                                        password As String, _
                                                        exchangeVersionsNummer As String) As ExchangeService
            Dim exchangeVersion As String = "ExchangeVersion." + exchangeVersionsNummer
            Dim exchange As ExchangeService
            exchange = New ExchangeService(exchangeVersion)
            exchange.Credentials = New WebCredentials(userName, password)
            exchange.UseDefaultCredentials = True
            Return exchange
        End Function

    If hard code 

    exchange = New ExchangeService(ExchangeVersion.Exchange2007_SP)

    The application works fine, but I get an error when using any variation of passing this argument as a string (as shown in the code snippet). I also tried passing the argument as Microsoft.Exchange.WebService.Data.ExchangeVersion objects, but that did not help either. As for the options available, I used the entries suggested by Visual Studio 2010 Intellisense. I got an error that a conversion to integer was not possible, however, I cannot see the connection why this object is converted to an integer.... (maybe somebody could explain this to me since I could not find any hints.) 

    I am aware of the issue(s) resulting from data type conversions, but I am not aware of any other workaround. The question is whether there is a) a way to pass those user-selected Exchange Server versions to the function and establish a connection accordingly or b) is there any other way to make this work under the assumption that Exchange Server version may be different on each user's machine.

    I hope the information provided are sufficient for you guys to assist me in this matter. I would like to thank you in advance for your support. I am looking forward to your suggestions.

    kind regards


    Friday, September 22, 2017 11:09 AM

All replies

  • Why don't you just cast the String as an Enum you can then just pass in the type expected eg

            Dim exchangeVersionNumber As String = "2013"
            Dim ExchangeVersionString As String = "Exchange" + exchangeVersionNumber
            Dim exchangeVersionEnum = DirectCast([Enum].Parse(GetType(ExchangeVersion), ExchangeVersionString), ExchangeVersion)
            Dim exchange As ExchangeService
            exchange = New ExchangeService(exchangeVersionEnum)

    Sunday, September 24, 2017 11:55 PM