blog

Returning an Anonymous type…

Published on
1 minute read

Returning an Anonymous type...   In VB.NET it is actually possible to return an Anonymous type from a function and use it somewhere else. In VB.NET you need to use late binding so you can’t change the “Option strict” compiler option to true as I have always done in the past. This option, that defaults to “Off”, is the main reason that C# developers scoff at VB, but this feature is now available, controversially in C#.

In this example I created an anonymous type that has three properties.

    Public Function GetTwiterCredentials() As Object
        If Not My.Settings.TwitterEmail.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterPassword.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterUsername.Length > 3 Then
            GetSettings()
        End If
        Return New With { _
                    .Email = My.Settings.TwitterEmail, _
                    .Password = My.Settings.TwitterPassword, _
                    .Username = My.Settings.TwitterUsername _
                        }
    End Function

The use of this is very simple, although I would like an option other than to return “Object” so Visual Studio knows that it is an anonymous type.

            Dim TwiterCredentials = View.GetTwiterCredentials
            Dim result As String = ""
            result = Twitter.TwitterAPI.UpdateStatus( _
                                    status, _
                                    TwiterCredentials.Email, _
                                    TwiterCredentials.Password _
                                    )

 

There is no intellisense with this, so you have to know what the options are. Hopefully in future versions this will be rectified.

Technorati Tags: .NET 

code-and-complexity blog code process

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Lockheed Martin Logo
Philips Logo
Milliman Logo
Schlumberger Logo
Xceptor - Process and Data Automation Logo
Illumina Logo
Jack Links Logo
Flowmaster (a Mentor Graphics Company) Logo
SuperControl Logo
Slicedbread Logo
MacDonald Humfrey (Automation) Ltd. Logo
Cognizant Microsoft Business Group (MBG) Logo

NIT A/S

Kongsberg Maritime Logo
YearUp.org Logo
Qualco Logo
Deliotte Logo
Teleplan Logo
Washington Department of Enterprise Services Logo
Washington Department of Transport Logo
Royal Air Force Logo
Department of Work and Pensions (UK) Logo
New Hampshire Supreme Court Logo
Ghana Police Service Logo
Brandes Investment Partners L.P. Logo
Sage Logo
Xceptor - Process and Data Automation Logo
Cognizant Microsoft Business Group (MBG) Logo
Alignment Healthcare Logo
Hubtel Ghana Logo