a·gen·tic a·gil·i·ty

Returning an Anonymous type

TL;DR; Explains how to return and use anonymous types from functions in VB.NET, highlighting late binding, Option Strict settings, and limitations like lack of IntelliSense.

Published on
2 minute read
Image
https://nkdagility.com/resources/ZD6hMyXVz9s
Subscribe

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.

 1    Public Function GetTwiterCredentials() As Object
 2        If Not My.Settings.TwitterEmail.Length > 3 Then
 3            GetSettings()
 4        End If
 5        If Not My.Settings.TwitterPassword.Length > 3 Then
 6            GetSettings()
 7        End If
 8        If Not My.Settings.TwitterUsername.Length > 3 Then
 9            GetSettings()
10        End If
11        Return New With { _
12                    .Email = My.Settings.TwitterEmail, _
13                    .Password = My.Settings.TwitterPassword, _
14                    .Username = My.Settings.TwitterUsername _
15                        }
16    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.

1            Dim TwiterCredentials = View.GetTwiterCredentials
2            Dim result As String = ""
3            result = Twitter.TwitterAPI.UpdateStatus( _
4                                    status, _
5                                    TwiterCredentials.Email, _
6                                    TwiterCredentials.Password _
7                                    )

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

Subscribe

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.​

Emerson Process Management Logo

Emerson Process Management

Deliotte Logo

Deliotte

New Signature Logo

New Signature

Illumina Logo

Illumina

Bistech Logo

Bistech

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Big Data for Humans Logo

Big Data for Humans

YearUp.org Logo

YearUp.org

Alignment Healthcare Logo

Alignment Healthcare

ALS Life Sciences Logo

ALS Life Sciences

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Healthgrades Logo

Healthgrades

Trayport Logo

Trayport

Higher Education Statistics Agency Logo

Higher Education Statistics Agency

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

ProgramUtvikling Logo

ProgramUtvikling

Jack Links Logo

Jack Links

Slaughter and May Logo

Slaughter and May

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Washington Department of Transport Logo

Washington Department of Transport

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Royal Air Force Logo

Royal Air Force

Ghana Police Service Logo

Ghana Police Service

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Healthgrades Logo

Healthgrades

Illumina Logo

Illumina

Qualco Logo

Qualco

Trayport Logo

Trayport

Flowmaster (a Mentor Graphics Company) Logo

Flowmaster (a Mentor Graphics Company)

YearUp.org Logo

YearUp.org