December 22, 2014
Hot Topics:

Declaring Anonymous Types in VB

  • December 7, 2009
  • By Paul Kimmel
  • Send Email »
  • More Articles »

Specifying a Key

Anonymous types support defining a key too. Every field in an anonymous type is readable and writable by default. You can specify that a field is the key. The key field is immutable as all such keys should be. The following code demonstrates how to employ the Key keyword.

Dim company = New With {Key .CustomerID = "ALFKI", .CompanyName = "Alfreds 
Futterkiste"}

In the revised version CustomerID is immutable. CustomerID will always be the value assigned upon initialization. If you check Reflector you will see that this is enforced in the generated code (see Figure 2).

ustomerID only has a getter and the CustomerID field is private as it should be
Figure 2: Note that CustomerID only has a getter and the CustomerID field is private as it should be.

Calling Parameterized Constructors and Using Named Initializers

If you have a pre-defined class, but want to use the initialize approach for fields that aren't defined in the constructor then you can combine a parameterized constructor with named initializers. (I am not using an anonymous type for this example.) Using the Customer class in Listing 2, the statement below calls the constructor that accepts a CustomerID and provides values for other public properties using the initialize list.

  Dim company3 As New Customer("PAULK") With {.CompanyName = "Kimmels 
  Konfections"}

If you step into the statement then you will see that it calls the Sub New(ByVal CustomerID As String) constructor and that the public property CompanyName is initialized too. Initalizers are supported for anonymous types and user- defined types.

Using Fields as Initializers

You also have the option for using earlier fields in initialize lists. For example, if the CompanyName-in our scenario-is identical to the CustomerID then you can provide a value for CustomerID and use the CustomerID to initialize the CompanyName. Here is the syntax for doing so.

Dim company4 As New Customer With {.CustomerID = "PAULK", .CompanyName = 
  .CustomerID}

In this example, I am using the named type Customer and setting CompanyName to CustomerID. Using fields in your initializer list is supported for anonymous types and user- defined type instantiation as shown above. Let's return to anonymous types.

Defining Nested Anonymous Types

Just as you can define composite types--classes containing fields that are other classes--long-hand, you can define anonymous types with nested types. To do so you use the initialize approach list and nest the initialization sequence. The following statement demonstrates this.

  Dim company5 = New With {.CustomerID = "PAULK", _
    .CompanyName = "Kimmel's Konfections", _
    .Phone = New With {.Number = "(517) 555-1212"}}

In the example notice that I have defined company5 as an anonymous type with three fields: CustomerID, CompanyName, and Phone. The difference here is that Phone is an anonymous type too. Simply repeat the usage of the "New With" construct for the nested type and provide the initializer list for the nested type. In this example the compiler will properly code generate two anonymous classes, one representing the outer type and the other representing the nested type. The nested type will be used as the type of the class property.

Summary

If one is performing a task then lazy is a relative term. The simple fact of using the easiest, sufficient approach does not make one lazy. I encourage you to use anonymous types wherever suitable. Think of anonymous types as a short hand for class syntax.

Biography

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book Professional DevExpress ASP.NET Controls (from Wiley) now available on Amazon.com and fine bookstores everywhere. Look for his upcoming book Teach Yourself the ADO.NET Entity Framework in 24 Hours (from Sams). You may contact him for technology questions at pkimmel@softconcepts .com. Paul Kimmel is a Technical Evangelist for Developer Express, Inc, and you can ask him about Developer Express at paulk@devexpress.com and read his DX blog at http:// community.devexpress.com/blogs/paulk.


Tags: Visual Basic, VB, VB.NET, anonymous types



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel