VB.NET如果语句与标准不一致? [英] VB.NET If statement is inconsistent with standard?

查看:78
本文介绍了VB.NET如果语句与标准不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这段代码:


Dim i As Integer = 0

Dim j As Integer = 5

If( j / i = 7)或i = 0然后

MessageBox.Show(In If)

Else

MessageBox.Show( In Else)

结束如果


我原本期望在执行If时抛出错误

语句,因为除法操作将导致除以零

异常。但这不会发生。以下声明在

之前添加了如果会导致异常...自然地:


j = j / i

什么规则声明If语句是免除的从错误?或者,更好

但是,我缺少什么?


TIA,


拉里伍兹

解决方案

Larry,

VB.NET如果语句与标准不一致?
If语句没有任何问题,它是你b $ b试图评估的表达式!


常规除法运算符/ "不会导致零除异常!

它返回一个基于IEEE 754算术的值。换句话说,它是
返回Nan,正无穷大或负无穷大。


你需要使用整数除法运算符\得分为零

例外。

http://msdn.microsoft.com/library/de...Spec11_5_4.asp


如果你的例子,常规除法运算符返回无限。 Infinity

不等于7,所以检查i = 0,它确实是。

If会导致异常...自然:
j = j / i


这会导致溢出异常,因为Infinity不适合32位

整数值。它不会导致Divide By Zero异常!


查看

Single&的Nan,PositiveInfinity和NegativeInfinity字段。双班。另请参阅单&的IsNan,IsInfinity,

IsPositiveInfinity和IsNegativeInfinity方法。双人

班。


希望这有帮助

Jay


" Larry伍兹" < LA *** @ lwoods.com>在留言中写道

新闻:u6 ************** @ tk2msftngp13.phx.gbl ...看看这段代码:

Dim i As Integer = 0
Dim j As Integer = 5
If(j / i = 7)或i = 0然后
MessageBox.Show(In If)
Else
MessageBox.Show(In Else)
结束如果

我希望在If语句因为除法运算会导致除以零
异常。但这不会发生。以下声明在
If之前添加。将导致例外...自然:

j = j / i

什么规则声明If语句是免除的从错误?或者,更好
然而,我错过了什么?

TIA,

拉里伍兹



这是因为1/0的结果是无穷大并且不等于7.


MessageBox.Show(j / i)显示Infinity

-

问候 - 一个垂头丧气的人


作者:Fish .NET&保持.NET

======================================= ==

此帖子按原样提供。没有保证,

并且不授予任何权利。

" Larry Woods" < LA *** @ lwoods.com>在留言中写道

news:u6 ************** @ tk2msftngp13.phx.gbl ...

看看这段代码:

Dim i As Integer = 0
Dim j As Integer = 5
If(j / i = 7)或i = 0然后
MessageBox.Show(" ;如果)
Else
MessageBox.Show(In Else)
结束如果

我本来希望在执行中抛出一个错误由于分割操作将导致除以零的异常,因此If
语句。但这不会发生。以下声明在
If之前添加。将导致例外...自然:

j = j / i

什么规则声明If语句是免除的从错误?或者,更好
然而,我错过了什么?

TIA,

拉里伍兹



该死!我会发誓我测试过了.....


谢谢,伙计们!


拉里


Jay B. Harlow [MVP - Outlook]" < JA ******** @ email.msn.com>在消息中写道

news:uK ************** @ tk2msftngp13.phx.gbl ...

Larry,

VB.NET如果语句与标准不一致? If语句没有任何问题,它是你试图评估的表达式!

常规除法运算符/不会导致零除以



异常!它返回基于IEEE 754算法的值。换句话说,它返回Nan,正无穷大或负无穷大。

你需要使用整数除法运算符\通过
零异常得到除法。

http://msdn.microsoft.com/library/de...Spec11_5_4.asp
如果您的例子,常规除法运算符返回无限。无穷大
不等于7,所以它会检查i = 0,它确实如此。

If会引起异常...自然:
j = j / i



这会导致溢出异常,因为Infinity不适合32位整数值。它不会导致Divide By Zero异常!

查看
Single& ;;的Nan,PositiveInfinity和NegativeInfinity字段。双班。还可以查看Single&的IsNan,IsInfinity,IsPositiveInfinity和IsNegativeInfinity方法。双层课程。

希望这有助于杰拉

拉里伍兹 < LA *** @ lwoods.com>在消息中写道
新闻:u6 ************** @ tk2msftngp13.phx.gbl ...

看看这段代码:

Dim i As Integer = 0
Dim j As Integer = 5
If(j / i = 7)或i = 0然后
MessageBox.Show(&In; If If) )
Else
MessageBox.Show(In Else)
结束如果

我本来希望在执行$ b时抛出错误$ b如果声明,因为除法操作将导致除以零
异常。但这不会发生。以下声明在如果之前添加了
。将导致例外...自然:

j = j / i

什么规则声明If语句是免除的从错误?或者,
更好,我缺少什么?

TIA,

拉里伍兹




Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn''t happen. The following statement, added before
the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?

TIA,

Larry Woods

解决方案

Larry,

VB.NET If statement is inconsistent with standard? There is nothing wrong with the If statement, its the expressions that you
are trying to evaluate!

The regular division operator "/" does not cause a Divide by Zero Exception!
It returns a value based on the IEEE 754 arithmetic. In other words it
returns Nan, Positive Infinity or Negative Infinity.

You need to use the integer division operator "\" to get a division by zero
exception.

http://msdn.microsoft.com/library/de...Spec11_5_4.asp

If your example, the regular division operator returns Infinite. Infinity
does not equal 7, so it checks if i = 0, which it does.
the "If" WILL cause an exception...natually:
j=j/i
This causes an Overflow Exception, as Infinity will not fit in a 32 bit
integer value. It does not cause a Divide By Zero exception!

Check out the Nan, PositiveInfinity, and NegativeInfinity fields of the
Single & Double classes. Also check out the IsNan, IsInfinity,
IsPositiveInfinity, and IsNegativeInfinity methods of the Single & Double
classes.

Hope this helps
Jay

"Larry Woods" <la***@lwoods.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl... Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn''t happen. The following statement, added before
the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?

TIA,

Larry Woods



It is because the result of 1/0 is infinity and that does not equal 7.

MessageBox.Show(j / i) displays Infinity
--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Larry Woods" <la***@lwoods.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl...

Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn''t happen. The following statement, added before
the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?

TIA,

Larry Woods



Darn! I would have sworn that I tested that.....

Thanks, guys!

Larry

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...

Larry,

VB.NET If statement is inconsistent with standard? There is nothing wrong with the If statement, its the expressions that you
are trying to evaluate!

The regular division operator "/" does not cause a Divide by Zero


Exception! It returns a value based on the IEEE 754 arithmetic. In other words it
returns Nan, Positive Infinity or Negative Infinity.

You need to use the integer division operator "\" to get a division by zero exception.

http://msdn.microsoft.com/library/de...Spec11_5_4.asp
If your example, the regular division operator returns Infinite. Infinity
does not equal 7, so it checks if i = 0, which it does.

the "If" WILL cause an exception...natually:
j=j/i



This causes an Overflow Exception, as Infinity will not fit in a 32 bit
integer value. It does not cause a Divide By Zero exception!

Check out the Nan, PositiveInfinity, and NegativeInfinity fields of the
Single & Double classes. Also check out the IsNan, IsInfinity,
IsPositiveInfinity, and IsNegativeInfinity methods of the Single & Double
classes.

Hope this helps
Jay

"Larry Woods" <la***@lwoods.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl...

Look at this code:

Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If

I would have expected to get an error thrown in the execution of the "If" statement since the division operation will cause a "divide by zero"
exception. But this doesn''t happen. The following statement, added before the "If" WILL cause an exception...natually:

j=j/i

What rule states that an If statement is "exempt" from errors? Or, better yet, what am I missing?

TIA,

Larry Woods




这篇关于VB.NET如果语句与标准不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆