BC30982: Type of '<variablename>' cannot be inferred because the loop bounds and the step variable do not widen to the same type
You have written a For...Next
loop in which the compiler cannot infer a data type for the loop control variable because the following conditions are 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.
No standard conversions exist between the data types.
Therefore, the compiler cannot infer the data type of a loop's control variable.
In the following example, the step variable is a character and the loop bounds are both integers. Because there is no standard conversion between characters and integers, this error is reported.
Dim stepVar = "1"c
Dim m = 0
Dim n = 20
' Not valid.
' For i = 1 To 10 Step stepVar
' Loop processing
' Next
Error ID: BC30982
To correct this error
Change the types of the loop bounds and step variable as necessary so that at least one of them is a type that the others widen to. In the preceding example, change the type of
stepVar
toInteger
.Dim stepVar = 1
-or-
Dim stepVar As Integer = 1
Use explicit conversion functions to convert the loop bounds and step variable to the appropriate types. In the preceding example, apply the
Val
function tostepVar
.For i = 1 To 10 Step Val(stepVar) ' Loop processing Next
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for