构造函数链接和空引用测试

发布于 2024-11-11 15:28:05 字数 407 浏览 3 评论 0原文

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

说:

  ' 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

How to test the null value BEFORE calling the other constructor?

Say:

  ' 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2024-11-18 15:28:05

我在 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 中执行非常类似的操作。 (共享方法?)

Reflector 向我保证这会转化为:

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

The way I do that in C# is by using a static method in the pipe, for example:

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)
{...}

I assume you can do something very similar in VB. (shared methods?)

Reflector assures me this translates into:

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
烛影斜 2024-11-18 15:28:05

一个丑陋的解决方法是扩展方法:

<Extension(), DebuggerNonUserCode()> _
Public Function EnsureNotNull(Of T As Class)(ByVal Value As T, _
                                             ByVal Arg As String) As T
    If Value Is Nothing Then Throw New ArgumentNullException(Arg)
    Return Value
End Function

使用如下:

' class MyHour '
Public Sub New(ByVal myHour As MyHour)
    Me.New(myHour.EnsureNotNull("myHour")._timeSpan)

End Sub

An ugly workaround would be an extension method:

<Extension(), DebuggerNonUserCode()> _
Public Function EnsureNotNull(Of T As Class)(ByVal Value As T, _
                                             ByVal Arg As String) As T
    If Value Is Nothing Then Throw New ArgumentNullException(Arg)
    Return Value
End Function

Used like:

' class MyHour '
Public Sub New(ByVal myHour As MyHour)
    Me.New(myHour.EnsureNotNull("myHour")._timeSpan)

End Sub
耀眼的星火 2024-11-18 15:28:05

处理这个问题的一个简单方法是使用“命名构造函数”的思想,也称为工厂方法。

Public Shared Function Create (ByValue myHour As MyHour) As Foo
  If myHour Is Nothing Then Throw New ArgumentNullException("myHour")
  Return New Foo(myHour._timeSpan)
End Function

Private Sub New(ByVal timeSpan As TimeSpan)
  '.... '
End Sub

您可以使用 System.Drawing.Color.FromArgb 查看类似的示例 使用工厂方法来避免歧义。

A simple way to handle this is to use the idea of "named constructors", aka factory methods.

Public Shared Function Create (ByValue myHour As MyHour) As Foo
  If myHour Is Nothing Then Throw New ArgumentNullException("myHour")
  Return New Foo(myHour._timeSpan)
End Function

Private Sub New(ByVal timeSpan As TimeSpan)
  '.... '
End Sub

You can see similar examples with System.Drawing.Color.FromArgb which use factory methods to avoid ambiguity.

星軌x 2024-11-18 15:28:05

我最初的冲动是建议您甚至没有采用 MyHour 实例的构造函数。相反,让用户检查类之外的任何内容:

Public Function GetSomeClassInstance(ByVal mh As MyHour) As SomeClass

    If mh IsNot Nothing Then
        Return New SomeClass(mh.TimeSpan)
    Else
        Throw New ArgumentNullException("mh", "MyHour instance must not be Nothing)
    End If

End Function

但是,使用私有构造函数实际构造对象,如下所示,可能会起作用(未经测试):

Public Class SomeClass
    Public Sub New(ByVal mh As MyHour)
        MyClass.New(Nothing, mh)
    End Sub

    Public Sub New(ByVal ts As TimeSpan)
        MyClass.New(ts, Nothing)
    End Sub

    Private Sub New(ByVal ts As TimeSpan?, ByVal mh As MyHour)
        Dim _timeSpanToUse As TimeSpan

        If ts IsNot Nothing Then
            _timeSpanToUse = ts.Value
        Else
            If mh IsNot Nothing Then
                _timeSpanToUse = mh.TimeSpan
            Else
                Throw New ArgumentNullException("mh", "The MyHour parameter was NULL")
            End If
        End If

        'Continue using _timeSpanToUse here...
    End Sub
End Class

My original impulse was to suggest that you not even have the constructor that takes the MyHour instance. Instead, make the user check for Nothing outside of the class:

Public Function GetSomeClassInstance(ByVal mh As MyHour) As SomeClass

    If mh IsNot Nothing Then
        Return New SomeClass(mh.TimeSpan)
    Else
        Throw New ArgumentNullException("mh", "MyHour instance must not be Nothing)
    End If

End Function

However, using a private constructor to actually construct the object, like this, might work (not tested):

Public Class SomeClass
    Public Sub New(ByVal mh As MyHour)
        MyClass.New(Nothing, mh)
    End Sub

    Public Sub New(ByVal ts As TimeSpan)
        MyClass.New(ts, Nothing)
    End Sub

    Private Sub New(ByVal ts As TimeSpan?, ByVal mh As MyHour)
        Dim _timeSpanToUse As TimeSpan

        If ts IsNot Nothing Then
            _timeSpanToUse = ts.Value
        Else
            If mh IsNot Nothing Then
                _timeSpanToUse = mh.TimeSpan
            Else
                Throw New ArgumentNullException("mh", "The MyHour parameter was NULL")
            End If
        End If

        'Continue using _timeSpanToUse here...
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文