【问题标题】:VB.NET assigning string value ---- String1 = String1+String2VB.NET 赋值字符串 ---- String1 = String1+String2
【发布时间】:2012-05-19 23:14:27
【问题描述】:

在 VB6 中,您可以将字符串的值重新分配给自身以及其他字符串值,例如:

str_Duplications_Line = str_Duplications_Line & pRow_Prime.Value(i_FieldNum)

现在,intellisense 不会将其视为错误,编译器也不会抱怨,但是当它运行时,它会在该行和其他多行(例如:

str_Duplications_Line = str_Duplications_Line & ","

str_Duplications_AllFields = str_Duplications_AllFields + str_Duplications_Line + vbCrLf

知道为什么会发生这种情况,我该如何解决这个问题?或者至少,在 VB.NET 中模拟相同的东西?

【问题讨论】:

  • “轰炸”?有错误吗?错误通常包含有关问题所在的有用信息。 +& 运算符都可用于连接字符串。有点相关:stackoverflow.com/questions/3006153/…
  • bombs out 是什么意思?你遇到了什么错误?

标签: vb.net string vb6


【解决方案1】:

这是&+之间的区别

"abc" + "def" = "abcdef"
"abc" & "def" = "abcdef"
"111" + "222" = "111222"
"111" & "222" = "111222"
"111" & 222 = "111222"
"111" + 222 = 333
"abc" + 222 = conversion error

如果任何一个操作数为空,你可能会得到一个错误。

【讨论】:

  • 当然如果你有 Option Strict On "111" + 222"abc" + 222 将不会编译
【解决方案2】:

确保您没有尝试将 null 连接到您的字符串

【讨论】:

  • 我会做一个 if-then 语句来确保它不会分配空值,谢谢!
  • 但是在VB中将null连接到字符串是否合法? AFAICR null 被视为空字符串“”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2023-03-28
  • 2014-09-09
  • 2019-12-29
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多