views:

63

answers:

3
Dim x As Integer = 1.8 \ 1 

Error:

Option Strict On disallows implicit conversions from 'Double' 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 ?!...

+2  A: 

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.

AakashM
from the same MSDN page:`Supported Types: All numeric types, including the unsigned and floating-point types and Decimal. ` in reality, only Long is supported as *expression1*.. :(
serhio
@serhio: Could it be that you are looking for the `/` operator instead?
0xA3
@divo: as you can see, x is an Integer, so I need a integer division. x = 1 or 2...mdeas...
serhio
+2  A: 

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.

Grzegorz Gierlik
Visual Basic attempts to convert any floating-point numeric expression to Long... So what should I do? MSDN tells also that all types are supported, but in reality is `Long` only?!.
serhio
You have to convert double to integer type. See @nobugz answer: http://stackoverflow.com/questions/2093067/operator-does-not-support-floats/2093386#2093386
Grzegorz Gierlik
+2  A: 

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)
Hans Passant
so, only integral operands are supported by \, not all(including floating point), how pretends MSDN.
serhio
@serhio: As Grzegorz Gierlik pointed out, you can use a floating point number only with Option Strict Off. It's clearly stated on MSDN.
Meta-Knight