无法从包含“<variablename>”的表达式推理出“<variablename>”的类型

如果在声明中使用变量建立其初始值,则编译器无法推断该变量的数据类型。

例如,在 Option Infer 设置为 On的情况下,下面的示例不编译:

  • 声明

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

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

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

错误 ID: BC30980

更正此错误

  • 如果两个变量用于指代不同的值,则更改正在声明的变量的名称。

  • 在赋值右侧的初始值中,使用文字值而不使用变量名。

  • 使用 As 子句来指定你要声明的变量的类型。

另请参阅