Type of '<variablename>' cannot be inferred from an expression containing '<variablename>'

The compiler cannot infer the data type of a variable if the variable is used in establishing its initial value in the declaration.

For example, with Option Infer set to On, the following examples do not compile:

  • Declarations

    ' Does not compile with Option Infer on.
    Dim m = m
    Dim d = someFunction(d)
    
  • For loop

    ' Does not compile with Option Infer on.
    For j = 1 To j
    Next
    
  • For Each loop

    ' Does not compile with Option Infer on.
    For Each customer In customer.Orders
    Next
    

Error ID: BC30980

To correct this error

  • If the two variables were intended to refer to different values, change the name of the variable that you are declaring.

  • Use a literal value instead of the variable name in the initial value, on the right side of the assignment.

  • Use an As clause to specify the type of the variable you are declaring.

See Also

Concepts

Local Type Inference

Reference

Dim Statement (Visual Basic)

For Each...Next Statement (Visual Basic)

For...Next Statement (Visual Basic)

Option Infer Statement