C# 3.0 vs. VB 9.0 and XML in the Language

Here is the summary of new language features as mentioned on the “What’s new on ORCAS

  • C# 3.0 Language Support: This CTP implements all of the C#3.0 language features from the May LINQ CTP including:

    • Query Expressions

    • Object and Collection Initializers

    • Extension Methods

    • Local Variable Type Inference and Anonymous Types

    • Lambdas bound to Delegates and Expression trees

    • Complete design-time support: Intellisense, Formatting, Colorization

  • VB 9.0 Language Support: This CTP contains the following language features:

    • Query Expressions: Basic querying, filtering, and ordering support

    • Object Initializers

    • Extension Methods

    • Local Variable Type Inference

    • Anonymous Types

    • XML literals

    • XML properties

    • New Line and Expression IntelliSense

LINQ is all in both of the languages and indeed this is the main feature for .NET Framework 3.5. Writing any type of queries is the purpose of LINQ at the end. Considering the abilities of LINQ, everything was possible before as well. LINQ makes us to get rid of the strings (the red coloured stuff) from the program in order to minimise the typo errors, easy to read programs by syntax highlighting. But all the best is that gives the ability to write declarative and functional style programs.

Beside the new language features, as a compiler improvement, it is very surprising that C# still doesn’t have background compilation. There is background syntax checking but no compilation. I believe this is a definite need for C# because it is really helpful. For instance the F# projects do background compilation and syntax checks, that way it easy to investigate “silly errors” while coding. Also this was one of the powerful features that I found on eclipse while working on a Java project.

Visual basic has that feature moreover it has also automatic syntax fixing as well. Likewise if you call a method with lower case letters it is automatically converted to the actual method on the next line. Actually in a type inferenced language this is needed, because it is not easy to recognise the type information of all the members.

Anyway I just wrote a quick macro to give the feeling of background compilation for C#. It is not really sophisticated but it works. Just put it into EnvironmentEvent macro in Visual Stuio.

Dim lastbuilt As DateTime
    Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean) Handles TextDocumentKeyPressEvents.AfterKeyPress
        Dim doc = DTE.ActiveDocument
        Dim diff = DateTime.Now.Subtract(lastbuilt)

        If Not Char.IsLetterOrDigit(Keypress(0)) And diff.Seconds > 5 Then
            lastbuilt = DateTime.Now
        End If
    End Sub

XML in Language

All of the best is that now XML is a first class citizen in VB. I wouldn’t expect this as a serious feature but after trials it makes extremely relevant to use XML in Visual Basic. You get syntax highlighting and even intellisense for XML if the namespaces are specified and even more.

Having XML literals in the language, it makes really sense to use XLinq with VB.

Like consider the xml stored by messenger. You could just assign to a variable just like that.

        Dim msn = 
                  <log firstsessionid="1" lastsessionid="1">
                      <message date="25/03/2007" sessionid="1" datetime="2007-03-25T21:35:47.173Z" time="22:35:47">
                          <from><user friendlyname="koko"></user></from>
                          <to><user friendlyname="opopop"></user></to>
                          <text style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">151515</text>
                      <message date="25/03/2007" sessionid="1" datetime="2007-03-25T21:35:55.344Z" time="22:35:55">
                          <from><user friendlyname="koko"></user></from>
                          <to><user friendlyname="opopop"></user></to>
                          <text style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">5959959</text></message>

It will have the type of System.Xml.Linq.XDocument.

Let’s define the XML Stylesheet :

        Dim xslt = 
                   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                       <xsl:template match="Log">
                                   <title> Message Log for </title>
                               <body style="margin:0">
                                   <table border="1">
                                           <td> From </td>
                                           <td> To </td>
                                           <td> Message </td>
                                       <xsl:for-each select="/Log/Message">
                                               <td><xsl:value-of select="[email protected]"></xsl:value-of></td>
                                               <td><xsl:value-of select="[email protected]"></xsl:value-of></td>
                                               <td><xsl:value-of select="Text"></xsl:value-of></td>

If we want to do an XSLT transformation to that snippet, it is even easier than it used to be.

Dim xTransform = New System.Xml.Xsl.XslCompiledTransform()
xTransform.Transform(msn.CreateReader(), New System.Xml.XmlTextWriter("test.html", New System.Text.UnicodeEncoding()))

I think working with xml data using Visual Basic should be considered as a manipulation language. Since we are all becoming multilingual this shouldn’t be a problem.