vb.net – 这些代码块之间的区别是什么,为什么前者失败了,我应该
我写:
MfgRecipeTypeKey = If(placeholderMRTK Is Nothing,placeholderMRTK) 并且一切正常,但是当placeholderMRTK实际上是Nothing时,它会失败,不会引发异常,只是退出sub(MyBase.Load以获取对话框表单)并继续使用该应用程序.当我把它重写为: If placeholderMRTK Is Nothing Then MfgRecipeTypeKey = 0 Else MfgRecipeTypeKey = placeholderMRTK End If 它工作正常.我认为这两者是合乎逻辑的等价物. 所以: 1)我不知道的两者之间的实际差异是什么? 2)为什么第一个失败?我有点想知道它是否是一个棘手的类型转换问题,但placeholderMRTK和MfgRecipeTypeKey都被声明为Byte? (可空字节)类型. 3)为什么执行只是脱离了sub而没有给我一个例外.当该行在Visual Studio中突出显示(Pro 2013如果重要)并且我f11用于下一行时,它只是跳出并运行数据网格渲染事件然后呈现我的对话框,但没有一些重要的数据分配发生在引擎盖下.鉴于它是这样做的(这是2013年的新行为吗?),我该怎么调试? 感谢您的时间和关注! 解决方法
您使用的
If() operator with three arguments期望两个可能的分支返回相同类型的值.
使用时并非如此 MfgRecipeTypeKey = If(placeholderMRTK Is Nothing,placeholderMRTK) 因为placeholderMRTK的类型为Nullable(Of Byte),0的类型为Integer. If placeholderMRTK Is Nothing Then MfgRecipeTypeKey = 0 Else MfgRecipeTypeKey = placeholderMRTK End If 是有效的,因为VB.Net允许您隐式转换0到字节. 你可以用 MfgRecipeTypeKey = If(placeholderMRTK Is Nothing,CType(0,Byte),placeholderMRTK) 将0转换为字节,或简单地使用 MfgRecipeTypeKey = If(placeholderMRTK,0) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |