asp:Feature
LANGUAGES: C#
ASP.NET VERSIONS: 2.0
New Features in Visual Basic 9.0
Will the New Additions to Visual Basic Help Regain Lost Ground?
By Joydip Kanjilal
Visual Basic has been one of the most popular programming
languages to date. Granted, it was losing its popularity and was about to fade
off. But just when it seemed that it was the end of the road for one of the
most popular programming languages ever, Microsoft has put in a host of new
features in Visual Basic 9.0, which ships with VS.NET
2008. MSDN states, Visual Basic has always centered on building pragmatic,
data-oriented, line of business applications. While the move to .NET
brought the power of a unified framework and a managed platform to the
application developer, the next release of Visual Basic includes a set of
features that result in a profound effect on a developer s productivity when
building data-oriented applications. These language extensions introduce
general-purpose query facilities that apply to all sources of data, be it relational,
hierarchical object graphs, or XML documents.
This article takes a look at these additions to Visual
Basic.
So, What s New?
In this section we ll take a look at some of the most
important features that have been added to Visual Basic. Here s a list of the additions
to Visual Basic:
- Support for LINQ
- Lamda Expressions
- Nullable Types
- Partial Methods
- Extension Methods
- Object Initializers
The new version of Visual Basic supports LINQ, a new addition
to Microsoft .NET Framework 3.5. Here s a code snippet that illustrates how you
can query data using LINQ and VB.
Dim Employees = {New Employee With { .Code = 1, .Name =
"Joydip"}, _
New Employee With {.Code = 2, .Name = "David"}}
And, now, you can query as shown in this code snippet:
Dim query = From Employee In Employees Select Employee
For Each Employee In query
Console.WriteLine(Employee.Name)
Next
You can use Lambda expressions to define a nameless
function, then use it as shown in the code snippet
below:
Dim result = Function(myNumber) Math.Pow(myNumber, 1 / 3)
MsgBox(result(8))
Nullable types allow you to assign null values to value
types. You can find more about Nullable types in my article at: http://www.devx.com/dotnet/Article/35621.
You can define a Nullable type in Visual Basic in one of
the following three ways:
Dim flag? As Boolean
Dim flag As Boolean?
Dim flag As Nullable(Of Boolean)
The Boolean variable flag can accept one of these values:
True, False, or Nothing. You can then use the type in the same way you use
other types. Here s an example:
If flag Then
MsgBox("True")
ElseIf Not married
Then
MsgBox("False")
Else
MsgBox("Un-defined")
End If
Now, if the Boolean variable flag is set to a value of Nothing ,
the message Un-defined will be displayed.
And, here s another example:
Dim Employees = New Employee With {.Code = 1, .Name =
"Joydip", .PF = Nothing }
Recall that you had Partial Classes in the earlier version
of Visual Basic. Now we have Partial Methods, as well.
Also, there are now anonymous types to define your type
without having to define your class. Awesome, isn t it? Here s how you can do
it:
Dim objEmployee = New With { .ecode = 12, .ename =
"Joydip"}
Object Initializers enable you to initialize an object at
the time it is instantiated. Here s how you can use Visual Basic to initialize
an object using its properties at the time you instantiate:
Dim myObject = new MyClass with {.a = 100, .b = 200, .c = 300}
Conclusion
This article has had a look at the some of the most
important features in the new version of Visual Basic that have been introduced
as part of Visual Studio 2008. It is just the beginning; let s hope more will
come and help Visual Basic regain its popularity and gain community respect. Please
send me your comments. Happy reading!
Joydip Kanjilal is
a Microsoft MVP in ASP.NET. He is the author
of ASP.NET Data Presentation Controls Essentials (Packt Publishing; http://www.packtpub.com/asp-net-data-presentation-controls/book).
He has more than 12 years of industry experience in IT with more than six years
in Microsoft .NET and its related
technologies. He has authored articles for some of the most reputable sites,
like http://www.asptoday.com, http://www.devx.com, http://www.aspalliance.com, http://www.aspnetpro.com, http://www.sql-server-performance.com,
http://www.sswug.com, etc. Many of these
articles have been selected at http://www.asp.net
(Microsoft s Official Site on ASP.NET).
Joydip also was a community credit winner at http://www.community-credit.com a
number of times. He is currently working as a Senior Consultant in a reputable
company in Hyderabad, India. He has years of experience in designing and
architecting solutions for various domains. His technical strengths include C,
C++, VC++, Java, C#, Microsoft .NET, AJAX,
Design Patterns, SQL Server, Operating Systems, and Computer Architecture.
Joydip blogs at http://aspadvice.com/blogs/joydip
and spends most of his time reading books, blogging, and writing books and
articles. His hobbies include watching cricket and soccer, and playing chess.
Contact Joydip via e-mail at mailto:joydipkanjilal@yahoo.com
and view his MVP profile at https://mvp.support.microsoft.com/default.aspx/profile/joydip.