.net – 构造函数链接和空引用测试
发布时间:2020-12-17 07:30:22 所属栏目:百科 来源:网络整理
导读:如何在调用其他构造函数之前测试空值? 说: ' class MyHoyr ' Public Sub New(ByVal myHour As MyHour) ' Can't doing it here !!!! ' If myHour Is Nothing Then Throw New ArgumentNullException("myHour") ' Constructor call should be first ' Me.New(
如何在调用其他构造函数之前测试空值?
说: ' class MyHoyr ' Public Sub New(ByVal myHour As MyHour) ' Can't doing it here !!!! ' If myHour Is Nothing Then Throw New ArgumentNullException("myHour") ' Constructor call should be first ' Me.New(myHour._timeSpan) ' Here is too late... ' End Sub Private Sub New(ByVal timeSpan As TimeSpan) '.... ' End Sub 解决方法
我在C#中这样做的方法是在管道中使用静态方法,例如:
public MyHour(MyHour myHour) : this(GetTimeSpan(myHour)) {} private static TimeSpan GetTimeSpan(MyHour myHour) { if(myHour== null) throw new ArgumentNullException("myHour"); return myHour._timeSpan; } private MyHour(TimeSpan timeSpan) {...} 我假设你可以在VB中做一些非常相似的事情. (共享方法?) 反射器向我保证这转化为: Public Sub New(ByVal myHour As MyHour) Me.New(MyHour.GetTimeSpan(myHour)) End Sub Private Sub New(ByVal timeSpan As TimeSpan) End Sub Private Shared Function GetTimeSpan(ByVal myHour As MyHour) As TimeSpan If (myHourIs Nothing) Then Throw New ArgumentNullException("myHour") End If Return myHour._timeSpan End Function (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在Google云端平台中启用适用于iOS的Places API
- 使用ICU库中的正则表达式匹配关键字之间含有特殊字符的关键
- c# – 从互联网下载HTML后,字符串中的字符已更改
- async Nand flash 、sync nand flash 和 toggle nand flash
- React Native中Mobx的使用
- dealloc在Swift中
- swift – 如何让.adjustsFontSizeToFitWidth正常运行
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
- ios – Git – Xcode文件计划在Project Navigator’D’中删
- Flex – ResizeEvent.RESIZE的问题