views:

142

answers:

6

I am sure this is a simple question for you guys but I don't know what this developer is doing.

name = String.Format(MyStringBuilder + "");

If I convert this to VB I get the message "operator + is not defined for types system.text.stringbuilder and string". Same thing if I use &.

+2  A: 

That doesn't make sense to me because AFAICT passing only one argument to string.format doesn't do anything.

Adding "" to the stringbuilder just coerces it to a string.

name = MyStringBuilder.ToString(); would be how I'd do this in C#. Converting that statement to VB should be loads easier.

Broam
+4  A: 

In the strictest sense, MyStringBuilder in the original code could be a null instance, at which point making an explicit call to .ToString() would throw an exception. The code sample provided would execute properly, however. In VB, you may want to say

Dim name As String
If MyStringBuilder Is Nothing Then
   name = String.Empty
Else
   name = MyStringBuilder.ToString()
End If
Anthony Pegram
+1 - Good catch.
RobS
why not the following to reduce redundancy?dim name as string = string.emptyif mystringbuider isnot nothing then name = mystringbuilder.tostring()
Jim
Thanks Anthony and everyone else. It looked like an old VB trick to get around Nulls in database queries and I never thought I would see that in C#. I just didn't trust my instincts.Bill
Bill
@Bill, there are cleaner, more-intuitive ways to do that in C#. `string name = MyStringBuilder != null ? MyStringBuilder.ToString() : string.Empty;`, for one. But even in the original code sample, `string.Format` was superfluous.
Anthony Pegram
@Anthony,@Bill in VB it could be `Dim name As String = IIf(MyStringBuilder Is Nothing, "", MyStringBuilder.ToString())` In later versions of VB you can use `If` in place of `IIf`
MarkJ
@MarkJ, I tested IIf yesterday in 3.5, you still get a NullReferenceException.
Anthony Pegram
@Anthony Oops, yes. Only `If` would avoid the NullReferenceException because it only evaluates the argument it needs. My bad
MarkJ
+11  A: 

It looks as if the person who wrote it is attempting to force an implicit conversion of MyStringBuilder to a string using the + operator in conjuction with the empty string.

To perform this assignment in VB you only need:

name = MyStringBuilder.ToString()
Joel Etherton
+1  A: 

Use MyStringBuilder.ToString(). That will fix the problem.

Tom Cabanski
A: 

You're trying to concatenate a StringBuilder object and String together - that wouldn't work :)

name = String.Format(MyStringBuilder.ToString() + "");

This should compile correctly.

Mr Roys
A: 

In VB.NET you would use a "&" instead of a "+"

This line:

name = String.Format(MyStringBuilder + "");

Is causing an implicit cast of MyStringBuilder to string (using the ToString() method) in order to use the "+" operator. It's the same as:

name = String.Format(MyStringBuilder.ToString() + "");

which is the same as

name = MyStringBuilder.ToString();

which becomes this in VB.NET:

name = MyStringBuilder.ToString()
Andrew Lewis