Problem solve Get help with specific problems with your technologies, process and projects.

VB 9 Anonymous Types help create flexible objects

Use anonymous types in VB 9 to create objects without writing class definitions. Developers input properties rather than data type, and can choose how many columns, and what order, for LINQ queries.

You can use anonymous types in Visual Basic 9.0 to create objects without specifically writing class definitions. Be aware that using anonymous types requires you to change your methods slightly when it comes to language-integrated queries (LINQs).

Anonymous types are one of several tools new within VB 9.0. True to their name, they allow developers to create objects without the need to name them or to define their class. What the developer does is input properties (in place of definitions), give them values, and decide which to call "key." The compiler takes a look at these properties and invents an appropriate class for the data type.

The input of these properties is the one area that the developer controls. Ultimately, anonymous types are a tool for developers who need more control over properties but no control over defining data type.

Another important function of these properties is deciding equality between two or more instances of different anonymous types. If the instances have properties that are equal in number and value, and if the same properties are considered "key," then you can conclude that the instances are equal.

Note that the mechanics of a LINQ query change slightly with anonymous -- as opposed to named -- types. You can choose how many columns of results you want and what order those results should appear in, but may not choose the data type.

Remember that anonymous types are useful only in certain circumstances. Anonymous types should be most effective when developers want control over properties, but don't need control over other aspects. In any case, anonymous types mark an increase in flexibility for VB 9.0. Class definition, no longer a stringent requirement, can be eliminated when necessary.

Full information can be accessed on the MSDN site's Anonymous Types page.

This was last published in July 2008

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.