Visual Basic -- NEXT (by Amanda Silver)

Visual Basic -- NEXT (by Amanda Silver)

  • Comments 8

The first preview of the next version of Visual Basic goes live this morning! I’m sitting in Bill and Jim’s keynote right now, waiting for our announcement to be made public. The release is part of the .NET Language Integrated Query Framework, code-named “project LINQ”, which allows Visual Basic (and C#) developers to their language for querying over data in databases, objects in memory, and XML. 

 

But there’s much more that’s in store for VB than just query.

 

The bits can be downloaded here and work on top of the most recent CTP that was released to MSDN on Monday. The samples shipped with this first release are focused on the deep XML integration. 

 

For this first entry, I’ll provide a survey of the language features that are exposed in this first release. I won’t go into the features in great depth; I’ll save that for later entries.

 

Type Inference – Allows the compiler to inspect the expression on the right-hand side of an assignment statement to infer the type of the variable.

Dim int = 1

Dim doub = 1.2

Dim str = "Hello"

Dim intArr = {1, 2, 3}

Dim strArr = {{"Fred", "Barney"}, {"George", "Judy"}}

Dim strBuilderArr = {New StringBuilder(), New StringBuilder()}

Dim objArr = {"Fred", 10}

 

The type inference feature extends to other statements in VB:

For Each Dim cust In Customers

   MsgBox("Customer Name: " & cust.Name)

Next

 

Object Initializers – Enables an extremely concise syntax for creating and initializing an Object. The compiler will call the parameterless constructor and assign to the properties. So, assuming we have a Class or Structure named Customer with two properties, Name and Address that are both String types, I can write:

Dim cust As Customer = {Name := "Nancy", Address := "123 Main St."}

 

Which translates to:

Dim cust As Customer = New Customer()

cust.Name "Nancy"

cust.Address = "123 Main St."

 

Query syntax – I really think the code speaks for itself…

Dim primes = {1, 2, 3, 5, 7, 9, 11, 13}

Dim primesOverFive = Select p From p In primes Where p > 5

 

But one of the most powerful aspects of this feature is that you can now query over all CLR objects. I think the following query illustrates this best:

Dim procs = Select p.ProcessName _

            From p In Process.GetProcesses() _

            Where p.Threads.Count > 6

 

XML Literals – Based on the XLinq framework, the next version of Visual Basic includes deep support for constructing, inspecting, transforming, and querying  XML.  You can write the XML the way to want to see it directly in the Visual Basic language. You can even invoke expressions within the XML literal as I do in getting the values for the birthday from Microsoft.VisualBasic.Today.

 

Dim nancyD = <Person Birthdate=(Microsoft.VisualBasic.Today)>

Nancy Davolio

 </Person>

 

XML Late Binding – Assuming I’ve got a bit of XML with several elements like that above, I can bind to its members – both elements and attributes with the code:

For Each Dim peep In people...Person

  MsgBox("Name: " & peep.Value & " Birthday: " & peep.@Birthdate.Value)

Next

 

Dynamic Identifiers – With this feature, the identifier you’d like to bind to can be determined at runtime. This is incredibly powerful for meta-data driven scenarios in which you’d like to bind to a particular property that’s determined by input provided at runtime, like reading an XML schema.  Assuming I’ve got an object with two Subs on it, “PrintHello” and “PrintHi”. With the following bit of code, the member that’s invoked is dependent on the result of Console.ReadLine.

Dim t As Object = New PrintGreeting

t.(Console.ReadLine())()

 

Relaxed Delegates – In the next version of Visual Basic you will be able to hook up event handlers to events that have different signatures:

Sub txtBoxHandler(ByVal sender As Object, ByVal e As EventArgs) _

Handles txtBox.Click, txtBox.KeyPress

      ‘ Handling code

End Sub

 

I’m incredibly excited to be able to start discussing these features with you. Please download the bits, play, and enjoy!!

 

(working on better formatting.)

 

- Amanda

 

 

Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
Page 1 of 1 (8 items)