如何转换整数?到VB.NET中的整数?

Nib*_*Pig 6 vb.net

我是C#程序员,但我正在将一些代码从C#转换为VB.NET.在C#中,我可以简单地使用(int)blah.getValue(),其中getValue()返回Integer?

DirectCast()在VB.NET中做一个不起作用,说Integer?无法转换为Integer.

想法?

Rha*_*ody 8

使用value属性获取实际的整数值.

Dim intNullable As Integer?

If intNullable.HasValue Then
 Return intNullable.Value
End If
Run Code Online (Sandbox Code Playgroud)


Lia*_*amB 6

整数?是可以为空的类型,因此您可能必须将其转换为可以为空的Integer.

你想使用像这样的CType函数,'Integer'或'Integer?' 根据您的情况而定

Val = CType(Source,Integer?)
Run Code Online (Sandbox Code Playgroud)

  • -1这个答案并不完全正确.null的行为不同.`(int)blah.getValue()`如果`blah.getValue()`为null,则抛出异常.如果`blah.getValue()`为null,`CType(blah.getValue(),Integer)`返回零. (5认同)