.net中构造函数如何处理空引用链接测试?

2026-05-08 12:393阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计266个文字,预计阅读时间需要2分钟。

.net中构造函数如何处理空引用链接测试?

在调用其他构造函数之前,如何测试空值?

说:'MyHour' + 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 + used + correctly

如何在调用其他构造函数之前测试空值?

说:

.net中构造函数如何处理空引用链接测试?

' 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

本文共计266个文字,预计阅读时间需要2分钟。

.net中构造函数如何处理空引用链接测试?

在调用其他构造函数之前,如何测试空值?

说:'MyHour' + 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 + used + correctly

如何在调用其他构造函数之前测试空值?

说:

.net中构造函数如何处理空引用链接测试?

' 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