Udostępnij za pośrednictwem


Type of '<variablename>' is ambiguous because the loop bounds and the step variable do not widen to the same type

Your code contains a For...Next loop in which the compiler cannot infer a data type for the loop control variable because the following conditions is true:

  • The data type of the loop control variable is not specified with an As clause.

  • The loop bounds and step variable contain at least two data types.

  • More than one possible conversion exists between the data types.

  • There is no best type among the candidates, so that the choice of type for the loop control variable is ambiguous.

For example, the following loop has one loop bound of type Integer and one loop bound of type UInteger:

Dim m As Integer = 1
Dim n As UInteger = 10
' Not valid.
' For i = m To n
    ' Loop processing.
' Next

There is a possible conversion between Integer and UInteger, and a possible conversion between UInteger and Integer, but both are narrowing conversions so neither is the best choice.

In the next example, a third variable of type Double is added. Both Integer and UInteger have standard widening conversions to Double, which makes conversion to Double the best choice. Type inference assigns to loop control variable i the data type Double.

Dim stepVar As Double = 1
' Valid.
For i = m To n Step stepVar
    ' Loop processing.
Next

Error ID: BC30983

To correct this error

  • Explicitly convert one of the variables to a type that the others have a widening conversion to, for example:

    For i = m To CLng(n)
    
  • Use an As clause to specify the type of the control variable:

    For i As Double = m To n 
    

See Also

Concepts

Implicit and Explicit Conversions

Local Type Inference

Widening and Narrowing Conversions

Reference

For...Next Statement (Visual Basic)

Option Infer Statement