Tip

VB 9 Anonymous Types help create flexible objects

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 first published in July 2008

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.