选项严格打开 [英] Option Strict On

查看:63
本文介绍了选项严格打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个操作,我将一个整数除以另一个整数并将

结果分配给第三个整数。但是,我收到错误消息:Option Strict

On禁止从''Double''到''Integer''的隐式转换。我知道这可以通过使用CInt来避免
;但是,如果我关闭Option Strict会对我的

代码的工作方式产生什么影响? - 将double转换为

整数以及其他操作的影响?


谢谢。

Hi,

I have an operation in which I divide one integer by another and assign the
result to a third integer. However, I get the error message: "Option Strict
On disallows implicit conversions from ''Double'' to ''Integer''." I know this
can be avoided by using CInt; however, what would be the affect in how my
code works if I turn Option Strict off?--the effect on converting double to
integer as well as on other operations?

Thanks.

推荐答案

选项Strict有助于防止您编写可能给出的代码

运行时错误。它通过确保铸件定义良好来实现这一点

并且允许。它还可以帮助您防止数字

类型之间的数据丢失。


在您的示例中。整数dividion将导致双重潜在,因此它试图隐式转换为那个。因为你有OSTRICT = ON,

编译器不允许这样做。如果你真的想要结果整数,

然后使用CType(Int1 / Int2,Integer)。


例子。

''Option Strict是ON

Dim int1,int2,int3 As Integer

int1 = 2

int2 = 3


''转换为Integer的结果

int3 = CType(int1 / int2,Integer)

MessageBox.Show(int3.ToString() )


''转换为Integer的结果

MessageBox.Show(CType(int1 / int2,Double).ToString())


我的建议,保持开启。


问候 - OHM

Nathan写道:
Option Strict helps prevent you from writing code which is likely to give
you runtime errors. It does this by making sure that casting is well defined
and permissable. It also helps you to prevent data loss between numeric
types.

In your example. A integer dividion will result in a double potentially, so
it tries to implicitly convert to just that. Because you had OSTRICT = ON ,
the compiler disallowed this. If you really want the result in the integer,
then use CType( Int1/Int2, Integer ).

Example.
''Option Strict is ON
Dim int1, int2, int3 As Integer
int1 = 2
int2 = 3

''Casts Result of division to Integer
int3 = CType(int1 / int2, Integer)
MessageBox.Show(int3.ToString())

''Casts Result of division to Integer
MessageBox.Show(CType(int1 / int2, Double).ToString())

My advice, leave it on.

Regards - OHM
Nathan wrote:
你好,

我有一个操作,我将一个整数除以另一个整数,并将结果分配给第三个整数。但是,我收到错误
消息:Option Strict On禁止从
''Double''到''Integer''的隐式转换。我知道这可以通过使用CInt来避免;然而,如果我转向
Option Strict off会影响我的代码如何工作? - 将double转换为整数的效果为
以及其他操作?

谢谢。
Hi,

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
''Double'' to ''Integer''." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?

Thanks.




问候 - OHM#OneHandedMan {at} BTInternet {dot} com



Regards - OHM# OneHandedMan{at}BTInternet{dot}com




第二位应该读取


''将分割结果转换为((双))

MessageBox.Show(CType(int1 / int2,Double).ToString())


不是


''演员表分割结果((整数))

MessageBox.Show(CType(int1 / int2,Double).ToString())


问候 - OHM

单手人[OHM#]写道:

Second bit should read

''Casts Result of division to ((Double))
MessageBox.Show(CType(int1 / int2, Double).ToString())

Not

''Casts Result of division to ((Integer))
MessageBox.Show(CType(int1 / int2, Double).ToString())

Regards - OHM
One Handed Man [ OHM# ] wrote:
选项Strict有助于防止您编写可能会给您带来运行时错误的代码。它通过确保铸件定义明确且允许使用来实现这一点。它还可以帮助您防止数字类型之间的数据丢失。

在您的示例中。整数dividion将导致双重潜在,因此它会尝试隐式转换为那个。因为你有OSTRICT = ON,编译器不允许这样做。如果你真的希望结果在整数中,那么使用CType(Int1 / Int2,Integer)。

例子。
''选项严格打开
昏暗int1,int2,int3 As Integer
int1 = 2
int2 = 3
''转换为Integer的结果
int3 = CType(int1 / int2,Integer )
MessageBox.Show(int3.ToString())
''转换为Integer的结果
MessageBox.Show(CType(int1 / int2,Double).ToString( ))

我的建议,保持开启。

问候 - OHM

Nathan写道:
Option Strict helps prevent you from writing code which is likely to
give you runtime errors. It does this by making sure that casting is
well defined and permissable. It also helps you to prevent data loss
between numeric types.

In your example. A integer dividion will result in a double
potentially, so it tries to implicitly convert to just that. Because
you had OSTRICT = ON , the compiler disallowed this. If you really
want the result in the integer, then use CType( Int1/Int2, Integer ).

Example.
''Option Strict is ON
Dim int1, int2, int3 As Integer
int1 = 2
int2 = 3

''Casts Result of division to Integer
int3 = CType(int1 / int2, Integer)
MessageBox.Show(int3.ToString())

''Casts Result of division to Integer
MessageBox.Show(CType(int1 / int2, Double).ToString())

My advice, leave it on.

Regards - OHM
Nathan wrote:


我有一个操作,我将一个整数除以另一个整数,并将结果分配给第三个整数。但是,我收到错误
消息:Option Strict On禁止从
''Double''到''Integer''的隐式转换。我知道这可以通过使用CInt来避免;然而,如果我转向
Option Strict off会影响我的代码如何工作? - 将double转换为整数的效果为
以及其他操作?

谢谢。
Hi,

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
''Double'' to ''Integer''." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?

Thanks.



问候 - OHM#OneHandedMan {at} BTInternet {dot} com



Regards - OHM# OneHandedMan{at}BTInternet{dot}com




问候 - OHM#OneHandedMan {at} BTInternet {dot} com



Regards - OHM# OneHandedMan{at}BTInternet{dot}com


" Nathan" < NK ********************* @ softhome.net> schrieb
"Nathan" <nk*********************@softhome.net> schrieb

我有一个操作,我将一个整数除以另一个整数,并将结果分配给第三个整数。但是,我收到错误
消息:Option Strict On禁止从
''Double''到''Integer''的隐式转换。我知道这可以通过使用CInt来避免;然而,如果我转向
Option Strict off会影响我的代码如何工作? - 将double转换为整数的效果为
以及其他操作?

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
''Double'' to ''Integer''." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?




为什么不试试呢?如果您使用\;作为整数除法运算符,错误

消失 - 即使选项严格打开。使用/,浮点

divison运算符,两个值都转换为双精度,然后分割,然后分配给变量的
。哔,错误=>结果必须转换。没有

选项严格 - 嗯,我不知道。如果我想进行转换,我会明确地将它转换为b $ b。否则编译器无法知道

转换是否有意。

-

Armin

< a rel =nofollowhref =http://www.plig.net/nnq/nquote.htmltarget =_ blank> http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote。 HTML



Why not try it? If you use "\" as integer division operator, the error
disappears - even with option strict on. Using "/", the floating point
divison operator, both values are converted to doubles, then divided, then
assigned to the variable. Beep, error => result must be converted. Wihtout
option strict - hmm, I don''t know. If I wanted to do a conversion, I''d
convert it explicitly. Otherwise the compiler can not know whether the
conversion is intended or not.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


这篇关于选项严格打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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