更多C#到VB [英] More C# to VB
问题描述
我知道在C#中, - 和++运算符可以充当前缀和后缀
运算符。考虑到这一点,
在将它们转换为VB后的两段代码之间会出现什么差异?
而(--n> = 0){
做一些代码
}
虽然(n - > = 0){
做一些代码
}
我不能直接进入VB,因为VB不允许在
语句,所以我必须在循环中的某处减少n。应该很容易,
我只是对于减少n的位置感到困惑。
I know that in C#, the -- and ++ operators can act as prefix and postfix
operators. With that in mind, what differences would appear between the
following two sections of code when translating them to VB?
While (--n >= 0) {
do some code
}
While (n-- >= 0) {
do some code
}
I can''t take these straight into VB, as VB doesn''t allow assignments within
statements, so I have to decrement n somewhere in the loop. Should be easy,
I''m just confused as to where to decrement n.
推荐答案
你好..前缀运算符递增。在
迭代之前的变量值,另一方面的postfix完全相同但是当循环
返回时。
问候,
-
AngelJ.HernándezM。
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
" ; OpticTygre" <运算******** @ adelphia.net> escribióenel mensaje
news:g4 ******************** @ adelphia.com ...
Hi there.. The prefix operator "increments" the variable value before
iterating, postfix in the other hand does exactly the same but when the loop
returns.
Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
"OpticTygre" <op********@adelphia.net> escribió en el mensaje
news:g4********************@adelphia.com...
我知道在C#中, - 和++运算符可以充当前缀和后缀
运算符。考虑到这一点,在将它们翻译成VB时,在两段代码之间会出现什么差异呢?
虽然(--n> = 0){
做一些代码
}
虽然(n - > = 0){
做一些代码
}
我可以''把这些直接带到VB中,因为VB不允许在语句中赋值,所以我必须在循环中的某处减少n。应该很容易,我只是对于减少n的位置感到困惑。
I know that in C#, the -- and ++ operators can act as prefix and postfix
operators. With that in mind, what differences would appear between the
following two sections of code when translating them to VB?
While (--n >= 0) {
do some code
}
While (n-- >= 0) {
do some code
}
I can''t take these straight into VB, as VB doesn''t allow assignments
within statements, so I have to decrement n somewhere in the loop. Should
be easy, I''m just confused as to where to decrement n.
" OpticTygre" <运算******** @ adelphia.net>在留言中写道
新闻:g4 ******************** @ adelphia.com ...
"OpticTygre" <op********@adelphia.net> wrote in message
news:g4********************@adelphia.com...
我知道在C#中, - 和++运算符可以充当前缀和后缀
运算符。考虑到这一点,在将它们翻译成VB时,在两段代码之间会出现什么差异呢?
虽然(--n> = 0){
做一些代码
}
虽然(n - > = 0){
做一些代码
}
我可以''把这些直接带到VB中,因为VB不允许在语句中赋值,所以我必须在循环中的某处减少n。应该很容易,我只是对于减少n的位置感到困惑。
I know that in C#, the -- and ++ operators can act as prefix and postfix
operators. With that in mind, what differences would appear between the
following two sections of code when translating them to VB?
While (--n >= 0) {
do some code
}
While (n-- >= 0) {
do some code
}
I can''t take these straight into VB, as VB doesn''t allow assignments
within statements, so I have to decrement n somewhere in the loop. Should
be easy, I''m just confused as to where to decrement n.
您需要根据以下内容更改n:
您的第一个示例使用了前缀减量运算符,其行为为
如下...
n的值递减,*然后*评估布尔表达式。
例如,如果n是== 0,它将递减到-1然后
问题是-1> = 0"将被要求并返回false。
您的第二个示例使用postfix减量运算符,其行为为
如下...
布尔表达式为评估,*然后*减去n的值。
例如,如果n是== 0,则问题0=0。会被要求和
返回true,*然后* n将减少到-1。
-
Peter [ MVP视觉开发者]
所有行业的杰克,无人掌握。
You''ll need to place the code to change n based on the following:
Your first example used the prefix decrement operator which behaves as
follows...
The value of n is decremented, *then* the Boolean expression is evaluated.
For example, if n were == 0, it would be decremented to -1 and then the
question "is -1 >= 0" would be asked and return false.
Your second example uses the postfix decrement operator which behaves as
follows...
The Boolean expression is evaluated, *then* the value of n is decremented.
For example, if n were == 0, the question "is 0 >= 0" would be asked and
return true, *then* n would be decremented to -1.
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Peter van der Goes ; < p _ ********** @ toadstool.u>在消息中写道
新闻:Oz **************** @ tk2msftngp13.phx.gbl ...
:
:" OpticTygre" <运算******** @ adelphia.net>在消息中写道
:新闻:g4 ******************** @ adelphia.com ...
:
:>我知道在C#中, - 和++运算符可以作为前缀和
:>后缀运算符。考虑到这一点,将以下两段代码转换为
:> VB?
$ b时会出现什么样的差异
:> $ b:>
:>虽然(--n> = 0){
:>做一些代码
:> }
:>
:>虽然(n - > = 0){
:>做一些代码
:> }
:>
:>我不能直接进入VB,因为VB不允许分配
:>在语句中,所以我必须在循环中的某个地方递减。
:>应该很容易,我只是对于减少n的位置感到困惑。
:
:您需要根据以下内容将代码更改为n以下:
:您的第一个示例使用了前缀减量运算符,其行为为
:跟随...
:
:n的值递减,*然后*布尔表达式是
:已计算。例如,如果n = = 0,则它将递减到-1
:然后问题是-1> = 0。会被要求并返回false。
:
:你的第二个例子使用postfix减量运算符,其行为为
:如下...... br />
:
:计算布尔表达式,*然后* n的值是
:递减。例如,如果n为== 0,则问题0=0
:将被要求并返回true,*则* n将递减为-1。 />
:
: -
:彼得[MVP视觉开发者]
:所有行业的杰克,无人掌握。
< piggybacking>
请注意,前/后递减发生在
布尔测试的上下文中。在这两种情况下,n在大括号内输入
逻辑之前递减。考虑以下C#代码:
-------------------------------- ------------------
使用系统;
公共班级{
public static void Main(){
int n = 2;
while( - n> = 0){
Console.WriteLine(" n =" + n);
}
Console.WriteLine(当循环退出时为&n; + n);
Console.WriteLine();
n = 2;
while(n--> = 0) {
Console.WriteLine(" n =" + n);
}
Console.WriteLine(&n;循环时退出:+ n);
}
}
---------------- ----------------------------------
这将生成以下控制台输出:
n = 1
n = 0
当循环退出时
n:-1
$ b $当循环退出时,bn = 1
n = 0
n = -1
n:-2
第一个情况下,n递减然后checke d看看它是否> = 0.
如果是,则输出到屏幕。这导致两次调用写入
控制台:
传递1:n递减为1
n = 1,所以布尔测试是真的
调用Console.WriteLine
传递2:n递减为0
n = 0,所以布尔测试是真的
调用Console.WriteLine
传递3:n递减为-1
n = - 1,所以布尔测试是假的
循环完成
在第二种情况下,进行测试以查看n> = 0并且只有
它递减了。这导致三次调用写入控制台:
传递1:n = 2,因此布尔测试为真
n递减为1
调用Console.WriteLine
传递2:n = 1,因此布尔测试为真
n递减为0
调用Console.WriteLine
传递3:n = 0,因此布尔测试为真
n递减为-1
调用Console.WriteLine
传递4:n = -1,所以布尔测试为假
循环完成
另外,由于n的值实际上是在执行布尔测试之后的逻辑之前递减的,因此将其转换为
vb是棘手的。这是因为在vb到++或 - 中没有直接对应的
语句。这个,imo,在
语言中是一个粗略的错误,应该更正。
为了获得与上面C#代码相同的结果,你需要
跟随vb逻辑:
--------------------------- -----------------------
进口系统
公共舱[等级]
Public Shared Sub Main()
Dim n As Integer = 2
Do until n< 0
n - = 1
如果n> = 0那么
Console.WriteLine(" n ="& n)
结束如果
循环
Console.WriteLine(当循环退出时为&n;& n)
Console.WriteLine()
n = 2
Do until n< -1
如果n> = 0那么
n - = 1
Console.WriteLine(" n ="& n)
否则
n - = 1
结束如果
循环
Console.WriteLine (当循环退出时为n:& n)
End Sub
结束班级
------- -------------------------------------------
丑陋且不直观。并且它不应该是必要的 - VB应该包括对++和 - 运算符的
支持。
Ralf
"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:Oz****************@tk2msftngp13.phx.gbl...
:
: "OpticTygre" <op********@adelphia.net> wrote in message
: news:g4********************@adelphia.com...
:
: >I know that in C#, the -- and ++ operators can act as prefix and
: >postfix operators. With that in mind, what differences would appear
: >between the following two sections of code when translating them to
: >VB?
: >
: > While (--n >= 0) {
: > do some code
: > }
: >
: > While (n-- >= 0) {
: > do some code
: > }
: >
: > I can''t take these straight into VB, as VB doesn''t allow assignments
: > within statements, so I have to decrement n somewhere in the loop.
: > Should be easy, I''m just confused as to where to decrement n.
:
: You''ll need to place the code to change n based on the following:
: Your first example used the prefix decrement operator which behaves as
: follows...
:
: The value of n is decremented, *then* the Boolean expression is
: evaluated. For example, if n were == 0, it would be decremented to -1
: and then the question "is -1 >= 0" would be asked and return false.
:
: Your second example uses the postfix decrement operator which behaves
: as follows...
:
: The Boolean expression is evaluated, *then* the value of n is
: decremented. For example, if n were == 0, the question "is 0 >= 0"
: would be asked and return true, *then* n would be decremented to -1.
:
: --
: Peter [MVP Visual Developer]
: Jack of all trades, master of none.
<piggybacking>
Note that the pre/post decrementation occurs in the context of the
boolean test. In both cases, n is decremented prior to entering the
logic inside the curly braces. Consider the following C# code:
--------------------------------------------------
using System;
public class Class{
public static void Main(){
int n = 2;
while(--n >= 0){
Console.WriteLine("n = " + n);
}
Console.WriteLine("n when the loop exits: " + n);
Console.WriteLine();
n = 2;
while(n-- >= 0){
Console.WriteLine("n = " + n);
}
Console.WriteLine("n when the loop exits: " + n);
}
}
--------------------------------------------------
This is generate the following console output:
n = 1
n = 0
n when the loop exits: -1
n = 1
n = 0
n = -1
n when the loop exits: -2
In the first case, n is decremented then checked to see if it is >= 0.
If so, it is output to the screen. This result in two calls to write to
the console:
Pass 1: n is decremented to 1
n = 1, so the boolean test is true
Console.WriteLine is called
Pass 2: n is decremented to 0
n = 0, so the boolean test is true
Console.WriteLine is called
Pass 3: n is decremented to -1
n = -1, so the boolean test is false
The loop is complete
In the second case, the test is made to see if n >= 0 and only then is
it decremented. This results in three calls to write to the console:
Pass 1: n = 2, so the boolean test is true
n is decremented to 1
Console.WriteLine is called
Pass 2: n = 1, so the boolean test is true
n is decremented to 0
Console.WriteLine is called
Pass 3: n = 0, so the boolean test is true
n is decremented to -1
Console.WriteLine is called
Pass 4: n = -1, so the boolean test is false
the loop is complete
Also, since the value of n is actually being decremented prior to
executing the logic that follows the boolean test, translating this into
vb is tricky. This is because there isn''t a direct corresponding
statement in vb to ++ or --. This, imo, is a gross oversite in the
language and should be corrected.
To achieve the same results as the C# code above, you''ll need the
following vb logic:
--------------------------------------------------
Imports System
Public Class [Class]
Public Shared Sub Main()
Dim n As Integer = 2
Do Until n < 0
n -= 1
If n >= 0 Then
Console.WriteLine("n = " & n)
End If
Loop
Console.WriteLine("n when the loop exits: " & n)
Console.WriteLine()
n = 2
Do Until n < -1
If n >= 0 Then
n -= 1
Console.WriteLine("n = " & n)
Else
n -= 1
End If
Loop
Console.WriteLine("n when the loop exits: " & n)
End Sub
End Class
--------------------------------------------------
Ugly and unintuitive. And it shouldn''t be neccesary - VB should include
support for the ++ and -- operators.
Ralf
>
这篇关于更多C#到VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!