将 C# 格式转换为 VB
我确信这对你们来说是一个简单的问题,但我不知道这个开发人员在做什么。
name = String.Format(MyStringBuilder + "");
如果我将其转换为 VB,我会收到消息“未为 system.text.stringbuilder 和 string 类型定义运算符 +”。如果我使用 & 也是一样。
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 &.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看起来编写它的人试图使用 + 运算符与空字符串结合来强制将 MyStringBuilder 隐式转换为字符串。
要在 VB 中执行此分配,您只需要:
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:
从最严格的意义上来说,原始代码中的 MyStringBuilder 可能是一个 null 实例,此时显式调用 .ToString() 将引发异常。但是,提供的代码示例可以正确执行。在VB中,你可能想说
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
这对我来说没有意义,因为 AFAICT 仅将一个参数传递给 string.format 不会执行任何操作。
将“”添加到字符串构建器只是将其强制为字符串。
name = MyStringBuilder.ToString();
这就是我在 C# 中执行此操作的方式。将该语句转换为 VB 应该会更容易加载。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.使用 MyStringBuilder.ToString()。这将解决问题。
Use MyStringBuilder.ToString(). That will fix the problem.
您正在尝试将 StringBuilder 对象和 String 连接在一起 - 这是行不通的:)
这应该可以正确编译。
You're trying to concatenate a StringBuilder object and String together - that wouldn't work :)
This should compile correctly.
在 VB.NET 中,您可以使用“&”而不是“+”
这一行:
导致 MyStringBuilder 隐式转换为字符串(使用 ToString() 方法),以便使用“+”运算符。它与以下内容相同:
相同:
与 VB.NET 中的以下内容
In VB.NET you would use a "&" instead of a "+"
This line:
Is causing an implicit cast of MyStringBuilder to string (using the ToString() method) in order to use the "+" operator. It's the same as:
which is the same as
which becomes this in VB.NET: