Returning an Anonymous type…

Audience

Everyone

image 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:  

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

Read more

Martin Hinshelwood
In organizational development and team dynamics, Agile (as the Agile Manifesto delineates) and Scrum (as the Scrum Guide outlines) guide teams not by solving their problems but by illuminating the issues that demand attention. These frameworks aim to identify and spotlight the challenges within a team or organization’s processes, effectively …
Martin Hinshelwood
This week, I participated in a Scrum.org Webinar hosted by Sabrina Love (Scrum.org Product Owner) as well as my colleagues, Joanna Płaskonka, Ph.D. and Alex Ballarin to discuss the state of learning and how immersive learning is the future of training. You can watch the video below to hear what …
Martin Hinshelwood
For a long time now I have been searching for that perfect domain that epitomised the vision, the why, of what I am trying to achieve with my customers and the industry at large. Now I have found it in http://nkdagility.com
Martin Hinshelwood
At the MVP Summit I was appalled by the number of people who asked questions about new features for supporting hierarchical tasks! I shared a disgusted look with Peter Provost and we had a quick (and I mean really quick) conversation that resulted in this post. it really comes down …