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.
精彩评论