You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET开启Option Strict On后遇绑定及类型推断错误,求代码排查指导

VB.NET错误排查与修复:Option Strict On相关问题

错误根源

  • Cannot infer an element type, and Option Strict On does not allow 'Object' to be assumed:声明inputBoxes时未指定数组类型,Option Strict On模式下编译器无法自动推断元素类型,且禁止默认使用Object类型兜底。
  • Option Strict On disallows late binding:数组元素被默认视为Object类型,访问tb.Text、inputBoxes(0).Select()属于后期绑定操作,而Option Strict On强制类型安全,禁止此类无明确类型的调用。

修复后的代码

' 明确指定数组类型为TextBox,解决类型推断问题
Dim inputBoxes As TextBox() = {
        BtxtBarcodeIN,
        BtxtcodeproductIN
 }

If inputBoxes.Any(Function(tb) tb.Text.Trim().Length = 0) Then
    MessageBox.Show("codeproduct in,Barcode cannot be empty.",
            "Report Status",
            MessageBoxButtons.OK,
            MessageBoxIcon.Warning)
    
    inputBoxes(0).Select()
    BtxtBarcodeIN.Focus() ' 调整顺序:原代码中Return会跳过此行,需放在Return前
    Return ' 或 Exit Sub
End If

额外注意点

原代码中Return语句位于BtxtBarcodeIN.Focus()之前,会导致焦点设置代码永远无法执行,需调整语句顺序。

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:12:05