2

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.

3 Answers 3

1

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

Dim myString As New System.Text.StringBuilder

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

m_rFlight.Layout = myString.ToString()
Sign up to request clarification or add additional context in comments.

2 Comments

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: 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.
1

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.

1 Comment

I would agree -- replace the "+" with "&" for string concatenation, or you may end up with some inadvertant numeric addition sometime.
0

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>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.