VB.net 中没有增量运算符

发布于 2024-11-15 10:33:08 字数 397 浏览 0 评论 0原文

我对 vb.net 相当陌生,在将 C# 中的 for 循环转换为 VB.net 时遇到了这个问题 我意识到增量运算符在 vb.net 中不可用(++ 和 --) 虽然我能够做类似 cnt +=1 的事情,但

我研究了一下并发现了 Eric 的帖子也有同样的内容,但并没有真正完全理解它。 他提到在 VB 中,语句不能只是一个表达式。不确定它如何真正适合。

我希望这里有人能够解释为什么它不能以与它相同的方式工作在 C# 中是这样的。 (希望这也适用,就像我们在 C# 中使用 == 进行比较一样)

I am fairly new to vb.net and came across this issue while converting a for loop in C# to VB.net
I realized that the increment operators are not available in vb.net (++ and --)
whereas i was able it do something like cnt +=1

I researched a bit and came across Eric's post on the same, but wasn't really able to understand fully on it.
He mentions of In VB, a STATEMENT cannot be just an EXPRESSION. not sure how that really fits in.

I hope someone here would be able to explain why this doesn't work in the same way as it does in C#.
(Hope this will also hold true as in why we have == in C# for comparison)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

奈何桥上唱咆哮 2024-11-22 10:33:08

我想说的是,在设计 Visual BASIC 时,语言设计者只是认为 BASIC 是比 C 更好的基线。您可以通过 C++Java追踪 C(以及更早的 BCPL)的沿袭C#.

VB 谱系来自 Dartmouth 的原始 BASIC(以及更早的 Fortran),并且是完全不同的野兽。

换句话说,最初作为令人尊敬的 BASIC:

LET I = I + 1

可能已经被黑客攻击并破坏了足够 :-)

根据 Eric 的帖子,i++; 是实际上只是一个表达式,它产生 i ,其副作用是 i 在事件发生后递增(类似于非副作用表达式 i;< /代码>)。

这是因为 C 允许这些裸露的表达式,甚至是像 42; 这样的东西,它并没有真正做太多事情,但完全有效。换句话说,下面是一个完整的C程序:

int main (void) { 1; 2; 3; 4; 5; 6; 7; 8; 9; return 0; }

所有这些表达式都是有效但无用的(当然除了最后的0)。

BASIC 中,这并没有真正完成,因为 BASIC语句(做某事的事情)组成。这就是为什么 i += 1 (一个递增 i 的语句)被认为是可以的,但是 i++ (一个什么也不做的表达式恰好有一个增加i的副作用)不是。你可能会说这只是语义上的吹毛求疵,但事实就是如此。

你应该感谢小小的怜悯,至少你不必处理 COBOL:

ADD 1 TO DD_WS_I.

I would say that the language designers simply thought that BASIC was a better baseline than C, when designing Visual BASIC. You can follow the lineage of C (and, earlier, BCPL) through C++, Java and C#.

The VB lineage comes from the original BASIC from Dartmouth (and, earlier, Fortran) and is a different beast altogether.

In other words, what started as the venerable BASIC:

LET I = I + 1

has probably been hacked and destroyed enough :-)

As per Eric's post, i++; is indeed just an expression, one that yields i with the side effect that i is incremented after the event (similar to the non-side-effect expression i;).

That's because C allows these naked expressions, even things like 42; which doesn't really do much but is perfectly valid. In other words, the following is a complete C program:

int main (void) { 1; 2; 3; 4; 5; 6; 7; 8; 9; return 0; }

All those expressions are valid but useless (except the 0 at the end of course).

In BASIC, this was not really done, because BASIC consisted of statements (things that did something). That's why i += 1 (a statement incrementing i) is considered okay, but i++ (an expression doing nothing which just happens to have a side effect which increments i) isn't. You could argue that it's just semantic hair-splitting but that's just the way it is.

You should be thankful for small mercies, at least you're not having to deal with COBOL:

ADD 1 TO DD_WS_I.
眼藏柔 2024-11-22 10:33:08

很简单,因为设计者认为当你有 i += 1 时,i++ 是不必要的。

For 循环不需要任何一个,因此您不会丢失任何内容。

毕竟它是视觉Basic...为什么要让它变得复杂呢?

Simply because the designers thought that i++ is unnecessary when you have i += 1.

For loops don't need either one, so you don't lose anything.

It's Visual Basic after all... why make it complicated?

夜巴黎 2024-11-22 10:33:08

正如 @paxdiablo 所说,在 VB 中(或者更确切地说,在它的祖先 BASIC 中),一切都曾经是一个语句。事实上,每条语句都是由一个关键字引入的。

因此,要分配我们拥有的变量

LET x = x + 1

并调用方法,我们必须

CALL SomeMethod

在VB中,LETCALL最终被删除(除了在一种特殊情况下),因为它完全是多余的并且不会增加清晰度。但 VB 的底层词法语法并没有发生太大变化:每个语句仍然必须是一个语句i++ 不是 VB 中的语句,因为它缺少函数调用或赋值。

在 VB.NET 的第一个版本中,是否要像 C# 那样引入前置和后置自增运算符存在争议。我们决定不这样做,原因很简单:无论如何都不建议在表达式中使用副作用。它通常会影响清晰度。因此,即使在 C# 中,在表达式中合法使用 i++ 也非常罕见,而合法使用 ++i 则更加罕见(尽管我赢了)不可否认,在某些情况下它增加了清晰度)。

在大多数情况下,您可以使用 i += 1 就可以了,这很好地表达了意图。

请注意,在 C++ 中,情况根本不同,因为这里(但在 C# 中不是!)i++ 实际上与 具有不同的语义 i += 1 由于运算符重载(在 C# 中我们也有运算符重载,但 ++ 不能重载)。

As @paxdiablo said, in VB (or rather, in its ancestor BASIC), everything used to be a statement. And in fact, every statement was introduced by a keyword.

So to assign a variable we had

LET x = x + 1

and to call a method, we had

CALL SomeMethod

In VB, the LET and CALL were finally dropped (except in one special circumstance) because it’s completely redundant and doesn’t add clarity. But the underlying lexical grammar of VB didn’t change all that much: each statement still has to be a statement. i++ isn’t a statement in VB, since it lacks either a function call or an assignment.

There was an argument in the first version of VB.NET whether to introduce pre- and post-increment operators like in C#. It was decided not to do this, for a fairly simple reason: using side-effects in expressions isn’t recommended anyway. It usually lets clarity suffer. So even in C# legitimate uses of i++ in an expression are very rare, and legitimate uses of ++i are rarer still (though I won’t deny that in some cases it adds clarity).

In most cases you can use i += 1 just fine and this perfectly well expresses the intent.

Notice that in C++, the situation is fundamentally different because here (but not in C#!) i++ actually has a different semantic than i += 1 due to operator overloading (in C# we also have operator overloading but ++ cannot be overloaded).

和影子一齐双人舞 2024-11-22 10:33:08

作为 VB 中表达式和语句之间区别的示例,
在 VB 中,以下代码会生成编译器错误,因为 count += 1count 加 1,但整个表达式 count += 1 不会返回结果,因此不能用作参数。

Dim count As Integer = 0
Console.WriteLine(count += 1)  ' compiler error

当然,这同样适用

Dim count As Integer = 0
count += 1
Console.Writeline(count)

于在字符串上使用 += 运算符。

“在 VB 中,语句不能只是表达式”是什么意思?

  • VB 编译器要求在某些赋值或其他操作中使用结果。
  • 因此,VB 中的赋值运算不会产生结果。如果这样做,VB 编译器将不允许它单独作为一条语句(编译器要求使用结果)。
  • 因此,VB 中的赋值可以用作语句,但不能用作表达式。也就是说,您不能使用赋值语句作为方法的参数,或作为中间结果。
  • 在 C# 中,赋值运算确实会产生一个值。因此,为了使赋值作为语句独立存在,编译器要求使用所有结果。
  • C# 中的推论是任何其他产生结果的操作都可以单独作为一条语句。例如,2 + 2 生成结果 4 并且可以单独作为一条语句,而在 VB 中则不能。

“为什么 VB 中不提供前置和后置增量运算符?”的简化答案

count++ 表示,首先返回 count 的值,然后增加 count (并且不将赋值的值返回给 count)。
在这种情况下,不使用递增的值(使用递增之前的值)。如前所述,VB 编译器要求您使用运算或为其赋值。

++count 表示,首先递增 count然后将赋值给 count< /代码>。
在这种情况下,将 +1 赋给 count 的值作为表达式的值返回。如前所述,VB 中的赋值不会产生结果。
因此,在 VB 中实现这些运算符会带来一些严重的痛苦。

As an example of the difference between expression and statement in VB,
in VB the following generates a compiler error since count += 1 increments count by 1, but the whole expression count += 1 does not return a result, so it can't be used as a parameter.

Dim count As Integer = 0
Console.WriteLine(count += 1)  ' compiler error

You have to do this instead

Dim count As Integer = 0
count += 1
Console.Writeline(count)

Of course same applies to using the += operator on a String.

What does "In VB, a statement cannot just be an expression" mean?

  • The VB compiler requires results to be consumed in some assignment or other operation.
  • Because of this an assignment operation in VB does not produce a result. If it did the VB compiler would not allow it to stand alone as a statement (the compiler requires results be consumed).
  • Thus assignments in VB can be used as statements, but not as expressions. That is you cannot use an assignment statement as a parameter to a method, or as an intermediate result.
  • In C# an assignment operation does produce a value. Thus in order for assignments to stand alone as statements, the compiler does not require all results to be consumed.
  • The corollary in C# is that any other operation that produces a result can stand alone as a statement. 2 + 2 for instance produces the result 4 and can stand alone as a statement, whereas in VB it can't.

Simplified answer to "Why are pre and post increment operators not available in VB?"

count++ says, first return the value of count, then increment count (and do not return the value of the assignment to count).
In this case the incremented value is not used (the value before incrementing is used). As mentioned before, the VB compiler requires you use or assign values of operations.

++count says, first increment count, then return the value of the assignment to count.
In this case, the value of assigning +1 to count is returned as the value of the expression. As mentioned before, assignments in VB do not produce a result.
Thus there would be some serious pain implementing these operators in VB.

南街九尾狐 2024-11-22 10:33:08

以下扩展方法复制 ++x x++ --x x--

Public Module INC_DEC

  <Runtime.CompilerServices.Extension>
  Public Function PreINC(ByRef x As Integer) As Integer
    Return Interlocked.Increment(x)
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PostINC(ByRef x As Integer) As Integer
    Dim tmp = x
    Interlocked.Increment(x)
    Return tmp
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PreDEC(ByRef x As Integer) As Integer
    Return Interlocked.Decrement(x)
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PostDEC(ByRef x As Integer) As Integer
    Dim tmp = x
    Interlocked.Decrement(x)
    Return tmp 
  End Function
End Module

The following extension methods replicate ++x x++ --x x--

Public Module INC_DEC

  <Runtime.CompilerServices.Extension>
  Public Function PreINC(ByRef x As Integer) As Integer
    Return Interlocked.Increment(x)
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PostINC(ByRef x As Integer) As Integer
    Dim tmp = x
    Interlocked.Increment(x)
    Return tmp
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PreDEC(ByRef x As Integer) As Integer
    Return Interlocked.Decrement(x)
  End Function

  <Runtime.CompilerServices.Extension>
  Public Function PostDEC(ByRef x As Integer) As Integer
    Dim tmp = x
    Interlocked.Decrement(x)
    Return tmp 
  End Function
End Module
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文