开发者

'\' operator does not support floats?

开发者 https://www.devze.com 2022-12-16 17:18 出处:网络
Dim x As Integer = 1.8 \\ 1 Error: Option Strict On disallows implicit conversions from \'D开发者_开发问答ouble\' to \'Long\'
Dim x As Integer = 1.8 \ 1 

Error:

Option Strict On disallows implicit conversions from 'D开发者_开发问答ouble' to 'Long'

What Long??

EDIT:

Apparently Visual Basic attempts to convert any floating-point numeric expression to Long. OK, this part is clear.

Now, I can use the

CType((Math.Round(myResultingSingle)), Integer)

but what for MSDN tells that \ operator supports all the types if in reality it supports only Long as expression1 ?!...


The integer division operator requires integral operands. Two possible ways to do it:

  • Dim x As Integer = CInt(1.8) \ 2
  • Dim x As Integer = CInt(1.8 / 2)


From the MSDN page \ Operator (Visual Basic) :

Before performing the division, Visual Basic attempts to convert any floating-point numeric expression to Long

That Long. Because Double to Long is a narrowing conversion, and Option Strict is on, you must explicitly ask for it to happen.


Here (in Remarks section) is the answer:

Before performing the division, Visual Basic attempts to convert any floating-point numeric expression to Long. If Option Strict is On, a compiler error occurs.

0

精彩评论

暂无评论...
验证码 换一张
取 消