将 C# 格式转换为 VB

发布于 2024-08-29 13:40:43 字数 205 浏览 5 评论 0原文

我确信这对你们来说是一个简单的问题,但我不知道这个开发人员在做什么。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吲‖鸣 2024-09-05 13:40:43

看起来编写它的人试图使用 + 运算符与空字符串结合来强制将 MyStringBuilder 隐式转换为字符串。

要在 VB 中执行此分配,您只需要:

name = MyStringBuilder.ToString()

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()
笔落惊风雨 2024-09-05 13:40:43

从最严格的意义上来说,原始代码中的 MyStringBuilder 可能是一个 null 实例,此时显式调用 .ToString() 将引发异常。但是,提供的代码示例可以正确执行。在VB中,你可能想说

Dim name As String
If MyStringBuilder Is Nothing Then
   name = String.Empty
Else
   name = MyStringBuilder.ToString()
End If

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
如何视而不见 2024-09-05 13:40:43

这对我来说没有意义,因为 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.

ゃ人海孤独症 2024-09-05 13:40:43

使用 MyStringBuilder.ToString()。这将解决问题。

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

东京女 2024-09-05 13:40:43

您正在尝试将 StringBuilder 对象和 String 连接在一起 - 这是行不通的:)

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

这应该可以正确编译。

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

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

This should compile correctly.

北方的韩爷 2024-09-05 13:40:43

在 VB.NET 中,您可以使用“&”而不是“+”

这一行:

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

导致 MyStringBuilder 隐式转换为字符串(使用 ToString() 方法),以便使用“+”运算符。它与以下内容相同:

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

相同:

name = MyStringBuilder.ToString();

与 VB.NET 中的以下内容

name = MyStringBuilder.ToString()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文