Hooray! Today we’ve finally shipped Visual Studio 2008, previously known as “Orcas”, previously known as some symbol which doesn’t have an ascii representation. We’ve come a long way, but it’s been a great ride. In his blog, Soma mentions many of the flagship features at a high-level that will undoubtedly make the every developer and head of IT salivate, but this blog is about Visual Basic. 🙂
This is a tremendous release for Visual Basic users everywhere as it’s the first to introduce query expressions and XML as a first class data-type. Not only does this make Visual Basic the most productive choice for programming data-centric applications, it also makes us VB developers the envy of many an alpha-geek. If you’d like to just try it out — be sure to check out Visual Basic Express which comes with all the features I talk about below.
With query the entire .NET frameworks become a queryable data source. You can now query against anything available in the .NET Frameworks – the registry, file system, live processes, etc. For example, the code below queries against the running processes and returns those with a thread count greater than 10, orders them by the thread count:
Dim query = From proc In Process.GetProcesses _ Let ThreadCount = proc.Threads.Count _ Where ThreadCount > 10 _ Order By ThreadCount _ Select proc.ProcessName, proc.Id, ThreadCount
Using the LINQ to SQL Object-Relational Mapping framework, you can query against a SQL database as though it was just another collection of objects exposed by the frameworks. The following query finds all the products that have been discontinued, groups them into categories, and finds the total number of units in stock for each category:
Dim db As New NorthwindDataContext(My.Settings.NORTHWNDConnectionString) Dim query = From prod In db.Products _ Where prod.Discontinued = True _ Group By prod.CategoryID _ Into Count(), NumInStock = Sum(prod.UnitsInStock) _ Join cat In db.Categories On cat.CategoryID Equals CategoryID _ Select cat.CategoryName, Count, NumInStock
Unlike writing huge SQL query strings, I get immediate feedback on the query I write.
I can use the same query language (VB!) to query against relational databases, XML, and collections of objects – I don’t need to learn any domain specific language, VB gives me access to all! It’s really easy for me to join across domains (I’ll take that up in a later blog post.)
With XML as a first class data-type, Visual Basic becomes a full XML transform language. But it’s also really easy for me to project to XML from my SQL database:
Dim query = From prod In db.Products _ Where prod.Discontinued = True _ Group By prod.CategoryID _ Into prodGroup = Group, NumInStock = Sum(prod.UnitsInStock) _ Join cat In db.Categories On cat.CategoryID Equals CategoryID _ Select <Products> <Category CategoryID=<%= CategoryID %> CategoryName=<%= cat.CategoryName %>> <%= From prod In prodGroup _ Select <Product Price=<%= prod.UnitPrice %>><%= prod.ProductName %></Product> %> </Category> </Products>
Last but not least, for those of you who aren’t so excited about the LINQ (Language INtegrated Query) project, Visual Basic 2008 comes with a tremendous improvement in the Intellisense experience overall. One of our MVPs said that he literally types two characters and hits tab, two characters, tab – it’s as though the code writes itself!
Obviously, we’re very excited about this release and hope it meets you with open arms eager to embrace the productivity! If you’re ready to go to the next level and dig in, be sure to check out our learning pages and How-Do-I videos which walk you through these features and so much more.
Tomorrow I’ll blog about the least often mentioned goodies hidden in Visual Basic 2008 so that you can become the guru master of VB9. ‘Til then…