views:

295

answers:

3

The following output produces a string with no closing xml tag.

m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString() + "</G3Grid:Spots>"

This following code works correctly

m_rFlight.Layout = m_rFlight.Layout + "<G3Grid:Spots>" + Me.gvwSpots.LayoutToString()
m_rFlight.Layout = m_rFlight.Layout + "</G3Grid:Spots>" 'add closing tag

What's going on here, what's the reason the first example isnt working and the second is?

The gvwSpots.LayoutToString() function returns a string.

+1  A: 

Consider the following code that should be the equivalent of your code:

Dim someString As String = String.Empty

someString = someString + "<G3Grid:Spots>" + "SomeValue" + "</G3Grid:Spots>"

Console.WriteLine(someString)

someString = String.Empty
someString = someString + "<G3Grid:Spots>" + "SomeValue"
someString = someString + "</G3Grid:Spots>"

Console.WriteLine(someString)

I tested it and in both cases the output is: <G3Grid:Spots>SomeValue</G3Grid:Spots>

If you don't get the same results then it's because either m_rFlight.Layout is not a string, or Me.gvwSpots.LayoutToString() doesn't return a string and does something strange with the + operator. You can use the & operator instead to make sure that only string concatenation is performed.

Meta-Knight
xpda
A: 

As Meta-Knight said, except that I would recommend using the StringBuilder class:

Dim myString As New System.Text.StringBuilder

myString .Append("<G3Grid:Spots>")
myStrinAppend(Me.gvwSpots.LayoutToString())
myString.append("</G3Grid:Spots>")

m_rFlight.Layout = myString.ToString()
Joel Etherton
So I ask why the code is doing something and you suggest using a stringbuilder?The code gets executed once for every user, using the stringbuilder for performance benefits won't be that usefull.The final string is no larger then a couple of 1000 characters.
Barfieldmv
@Barfieldmv: Yah, like I said, I agree with Meta-Knight. It looked to me like you're you having goofy append issues with your string possibly because of a typing problem. Using StringBuilder might help alleviate that by ensuring that the output is definitely a string. Other than that, you shouldn't be using + to concatenate (as other have said). I don't recommend StringBuilder for performance in this case. I recommend it for type safety.
Joel Etherton
A: 

You can use string.concat

m_rFlight.Layout = string.concat(m_rFlight.Layout, "<G3Grid:Spots>",_
  Me.gvwSpots.LayoutToString(), "</G3Grid:Spots>")

or, as Meta-Knight mentioned, & instead of +. (It will always convert to string before concatenation.)

m_rFlight.Layout &= "<G3Grid:Spots>" & Me.gvwSpots.LayoutToString() & "</G3Grid:Spots>"
xpda