![]() |
|
![]() ![]()
![]() |
|
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?
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.
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:
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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||