VB.NET中的隐式转换 [英] Implicit casting in VB.NET

查看:230
本文介绍了VB.NET中的隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题适用于懒惰的VB程序员.拜托.

The question is intended for lazy VB programmers. Please.

在vb中我可以做到,而且不会出现任何错误.

In vb I can do and I won't get any errors.

示例1

Dim x As String = 5
Dim y As Integer = "5"
Dim b As Boolean = "True"

示例2

Dim a As EnumType = 4
Dim v As Integer = EnumType.EnumValue

示例3

Private Sub ButtonClick(sender As Object, e As EventArgs)
    Dim btn As Button = sender        
End Sub

示例4

Private Sub ButtonClick(sender As Button, e As EventArgs)
    Dim data As Contact = sender.Tag
End Sub

如果我确实知道预期的运行时类型,是否禁止"依赖vb语言内置的强制转换? 我什么时候可以依靠?

If I surely know the expected runtime type, is this 'forbidden' to rely on the vb-language built-in casting? When can I rely?

推荐答案

对MarkJ的评论已针对每个OP进行了回答

随意依赖于您想要的所有内容,只需确保您知道隐式强制转换所执行操作的规则即可.就是说,示例#4在将来看起来真的很容易破解,如果以前至少进行过一次空检查,我会更加高兴.

Feel free to rely on it all you want, just make sure you know the rules for what the implicit cast is doing. That said, example #4 looks really easy to break in the future, I'd be much happier if there was at least a null-check before.

这篇关于VB.NET中的隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆