Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / Visual Basic

Mutliline String Literals in VB.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
30 Nov 2011CPOL2 min read 34.9K   12   6
How to have mutliline string literals in VB.NET.

For as long as I can remember, VB.NET programmers have had to deal with the frustration of multiline strings. I remember programming in ASP.NET and creating long functions which used a StringBuilder to put together my Strings. It was tedious, hard to read and harder to edit.

I've always liked C#'s multiline string literal capabilities, but VB by its very nature could never support such a construct (it would violate the basic syntax rules we've all come to know and love). I was glad to see the elimination of underscores in many scenarios (an ugly, overbearing line continuation marker that just added insult to injury), but the basic look and feel of multiline strings basically hasn't changed.

VB
Dim sourceText As String =
    "Imports Microsoft.VisualBasic" & vbNewLine &
    "Imports System" & vbNewLine &
    "Imports System.Collections" & vbNewLine &
    "Imports Microsoft.Win32" & vbNewLine &
    "Imports System.Linq" & vbNewLine &
    "Imports System.Text" & vbNewLine &
    "Imports Roslyn.Compilers" & vbNewLine &
    "Imports System.ComponentModel" & vbNewLine &
    "Imports System.Runtime.CompilerServices" & vbNewLine &
    "Imports Roslyn.Compilers.VisualBasic" & vbNewLine &
    vbNewLine &
    "Namespace HelloWorld" & vbNewLine &
    "  Module Program" & vbNewLine &
    "    Sub Main(args As String())" & vbNewLine &
    "      Console.WriteLine(""Hello, World!"")" & vbNewLine &
    "    End Sub" & vbNewLine &
    "  End Module" & vbNewLine &
    "End Namespace"

Until XML Literals.

I was recently perusing the new Roslyn project website when I noticed a very clever and novel way to support multiline strings in VB.NET. As hinted at in the code above, Roslyn is capable of code analysis (among many, many other things), which requires you to feed it some code. Normally you'd probably put the code you want to analyze in its own file and use a StreamReader to serve it up to Roslyn, but that would be too complicated for the code examples on the website. As an alternative, I discovered the following syntax:

VB
Dim sourceText As String =
    <string>
        Imports Microsoft.VisualBasic
        Imports System
        Imports System.Collections
        Imports Microsoft.Win32
        Imports System.Linq
        Imports System.Text
        Imports Roslyn.Compilers
        Imports System.ComponentModel
        Imports System.Runtime.CompilerServices
        Imports Roslyn.Compilers.VisualBasic
        
        Namespace HelloWorld
          Module Program
            Sub Main(args As String())
              Console.WriteLine("Hello, World!")
            End Sub
          End Module
        End Namespace
    </string>

At first I thought that the "string" tag may be special, but you can use any name you want. Converting an XElement to a String renders the content only. If you prefer type inference, you can use this syntax:

VB
Dim sourceText =
    <string>
        Imports Microsoft.VisualBasic
        Imports System
        Imports System.Collections
        Imports Microsoft.Win32
        Imports System.Linq
        Imports System.Text
        Imports Roslyn.Compilers
        Imports System.ComponentModel
        Imports System.Runtime.CompilerServices
        Imports Roslyn.Compilers.VisualBasic
        
        Namespace HelloWorld
          Module Program
            Sub Main(args As String())
              Console.WriteLine("Hello, World!")
            End Sub
          End Module
        End Namespace
    </string>.Value

Some clean up will be required. For example, all white-space is preserved (including indentations) and you'll probably want to remove the leading and trailing spaces. If you want a string that you could print directly to a Console or Message Box window without having to edit, you'll need to use something like the following format:

VB
Dim sourceText = <string>Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports Microsoft.Win32
Imports System.Linq
Imports System.Text
Imports Roslyn.Compilers
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports Roslyn.Compilers.VisualBasic

Namespace HelloWorld
  Module Program
    Sub Main(args As String())
      Console.WriteLine("Hello, World!")
    End Sub
  End Module
End Namespace</string>.Value
This article was originally posted at http://cyborgx37.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCDATA literals Pin
Tim Overbay22-Apr-13 12:20
Tim Overbay22-Apr-13 12:20 
GeneralMy vote of 5 Pin
BManfred2-Jul-12 23:38
BManfred2-Jul-12 23:38 
GeneralMy vote of 5 Pin
Daniel Kamisnki10-Jun-12 16:11
Daniel Kamisnki10-Jun-12 16:11 
QuestionMy Vote of 5 Pin
Clark Kent12320-Dec-11 4:57
professionalClark Kent12320-Dec-11 4:57 
GeneralMy vote of 5 Pin
Alessandro Bernardi4-Dec-11 7:40
Alessandro Bernardi4-Dec-11 7:40 
GeneralMy vote of 5 Pin
Graeme_Grant30-Nov-11 18:29
mvaGraeme_Grant30-Nov-11 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.