tags:

views:

434

answers:

6

This is probably really obvious and I'm being dense. In C# I can do this:

string = @"this is 
            some preformatted 
            text";

How do I do this in VB?

+12  A: 

There isn't one.

In C# you have the ability to do something like this "This ends in a new line\n.", but in VB there's no concept of that, you have predefined variables that handle that for you like "This ends in a new line" & vbNewLine

Hence, there's no point in a string literal (@"something\n") because in VB it would be interpreted literally anyway.

The problem with VB .NET is that a statement is deemed terminated at the end of a line, so you can't do this

Dim _someString as String = "Look at me
  I've wrapped my string
    on multiple lines"

You're forced to terminate your string on every line and use an underscore to indicate you wish to continue your statement, which makes you do something like

Dim _someString as String = "Look at me " & vbNewLine &_
   "*** add indentation here *** I've wrapped my string " & vbNewLine &_
   vbTab & " on multiple lines" '<- alternate way to indent
Joseph
The example is C#.
Stevo3000
Damn, ignore comment. I missread.
Stevo3000
that adds to my list of reasons why vb sucks
Micah
@MicahAre String literals that useful? I'm a VB developer and never felt like I would really benefit from that feature. I can't say the same about some other C# features like auto-properties though...
Meta-Knight
They can be. I personally "grew up" in a c++ java world so string literals to me come very naturally. There are some things you can do in string literals that, to me, are easier than the VB equivalent (like vertical tab). Here's a link to an appendix of string literals in C#: http://en.csharp-online.net/Appendix_B._CSharp_String_Literals
Joseph
You can, however, set the VB environment to wrap lines automatically so you're not shoving _ everywhere
CodeByMoonlight
+3  A: 

I don't think you can do this in VB. You have to do:

Dim text As String = "this is" & Environment.NewLine _
                     & " some preformatted" & Environment.NewLine _
                     & " text"

Edit: As suggested in comments, replaced VB specific vbNewLine by Environment.NewLine

Meta-Knight
environment.newline is available too.
Pondidum
+1 for Environment.NewLine (that also works in C# as well)
Joseph
Emvironment.NewLine works fine in both languages, but most coding standards will steer you towards using the language-specific implementations. The same applies to other things such as Int32, Int63, etc... just an FYI, there really isn't any value-added to using either
STW
+1  A: 

VB is weak for string manipulation. No pre-formatting or inline escape characters. Any speacial characters need to be appended to the string.

Stevo3000
VB is kind of weak in a lot of ways similar to this; however it's really not a downfall. VB.NET is much easier to learn since it's more descriptive, C# provides lots of shortcuts by means of special characters and the like--but it also makes it more difficult for a green developer to pick up. VB.NET is geared towards Rapid Application Development and less experienced developers; C# is geared towards enterprise/framework development and more advanced developers. Each serves very valid purposes, neither is "better" from an objective standpoint--just better at different things.
STW
@Yoooder - That was the point I was trying to get across, horses for courses. But as far as strings go, C# is clearly ahead of VB (at the expense of being less descriptive for beginners).
Stevo3000
+2  A: 

Actually you can do this in vb.net. You use something called XML Literals.

Dim mystring = <string>this is
 some preformatted
 text</string>.Value
devSpeed
The compliler, however, won't be able to optimize the construction of the xml element away. Use this sparingly!
SealedSun
+1  A: 

You might want to try Alex Papadimoulis' "Smart Paster" add-in. It lets you paste a string into C# or VB code "as StringBuilder".

Brian Schroer
+2  A: 

Like others said, there's no @ operator, so if you get into heavy string manipulation, use String.Format

IMHO, this

Dim text As String = String.Format("this is {0} some preformatted {0} text", Environment.Newline)

is more readable than this

Dim text As String = "this is" & Environment.NewLine _
                 & " some preformatted" & Environment.NewLine _
                 & " text"
Adam Neal