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.
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:
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:
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:
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
发表评论
Dear Cyq,
I am writing because Cyqdata could get featured in digital newspapers in August, this is the time when newspapers get the most traffic.
Being mentioned by digital newspapers not only boosts the reputation of your business, it also helps to enhance your search engine rankings and gives you more credibility and trustworthiness.
I’d like to explain how it works and how you can get publications free of charge. What phone number can I reach you on?Pretty! This has been an incredibly wonderful post. Thanks for supplying these details.
For the rest of us, it means better scores and lower golf handicaps.
I’ve learn a few good stuff here. Certainly price bookmarking for revisiting. I surprise how much effort you set to make this type of wonderful informative web site.
vIScf8 Thanks-a-mundo for the blog article. Keep writing.
b97YNM This is a great article in many ways. I can at express how impressed I am with your content. I appreciate your viewpoints and how you make it all so interesting.
9upEEh Pretty! This was an extremely wonderful article. Thanks for providing this information.
o3TZCC Thanks-a-mundo for the post.Much thanks again. Fantastic.
QZ7vlG Nice post here, thought I could learn more from but I can learn more from this post.
vNHicC This particular blog is without a doubt cool and also informative. I have picked up a lot of handy tips out of it. I ad love to go back again and again. Thanks a bunch!
jdeifB This particular blog is obviously entertaining as well as informative. I have found a lot of interesting things out of this blog. I ad love to go back every once in a while. Cheers!
18Mhec Some truly fantastic articles on this website , thanks for contribution.
SE0mkS seem like you know what you are talking about!
rm32qW Pretty! This has been a really wonderful article. Thank you for supplying this info.
yfL2lp Thanks a lot for the post.Much thanks again. Really Great.
G6mZkP please visit the internet sites we adhere to, like this one particular, because it represents our picks in the web
I never thought I would find such an everyday topic so eniathllrng!
G14cRS the book in it or something. I think that you
a1UjAu It as hard to come by well-informed people for this topic, however, you sound like you know what you are talking about! Thanks
tiENxZ Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is magnificent, let alone the content!
VukEtH Very good article! We will be linking to this particularly great post on our site. Keep up the good writing.
XsZMmr Perfectly indited content material , thankyou for information.
jDREqT Very good article.Really looking forward to read more. Fantastic.
sJ5YS1 that matches all of your pursuits and wishes. On a website primarily based courting earth-wide-internet
r1Z9NY The Search Engine Optimization services they provide are tailored to meet
TBTjVd
PNkSFp Quite right! It There is apparently a lot to realize about this. I suppose you made some good points in features also.
lfOkPA Major thankies for the article post.Really thank you! Really Great.
fSZ4Y4 Thank you for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next post thanks once again.
kVYj50 Your style is really unique in comparison to other people I have read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this page.
RtnU2g There as certainly a lot to know about this issue. I like all of the points you ave made.
Ci3zhY I am so grateful for your article.Really looking forward to read more. Awesome.
J1qKF4 Thanks for the auspicious writeup. It in truth used to be a amusement account it. Look complex to more added agreeable from you! By the way, how can we keep in touch?
dKNBHt excellent post, very informative. I wonder why the other experts of this sector don't notice this. You should continue your writing. I am confident, you have a huge readers' base already!
iktWAJ Wonderful site. A lot of helpful info here. I'm sending it to a few friends ans additionally sharing in delicious. And obviously, thanks for your sweat!
wfRUmI You can definitely see your skills in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always follow your heart.
ACYLC0 F*ckin' remarkable things here. I'm very glad to look your article. Thank you a lot and i'm looking ahead to touch you. Will you kindly drop me a e-mail?
JV0bMi Very efficiently written article. It will be valuable to anybody who usess it, including yours truly :). Keep up the good work - for sure i will check out more posts.
l8fc8v Say, you got a nice blog post.Really thank you! Really Great.
yrmBqu A round of applause for your article. Fantastic.
KYWzrK I loved your post.Thanks Again. Great.
zfFb6R Enjoyed every bit of your post.Really thank you! Cool.
T9jEIX Thanks a lot for the blog post.Much thanks again. Cool.
KhPO7P Thanks a lot for the article post.
mLT7uV Thanks a lot for the article.Really thank you! Fantastic.
cDNeBz A big thank you for your blog post.Really thank you! Fantastic.
EwqkyA Enjoyed every bit of your blog.Thanks Again.
815T64 This is one awesome article post.Thanks Again. Keep writing.
UjY30Q Very neat article post.Thanks Again. Really Great.
V3TLg6 Im obliged for the article.Really looking forward to read more. Will read on...