如何创建一个允许我将未分配的属性传递给方法而没有错误的构造函数?

时间:2014-07-10 09:17:23

标签: .net vb.net oop constructor

在下面的代码中,我将几个参数传递给方法“ SendEmail() ”。

未分配的字符串属性 myEmail.ToAddress.Name 没有问题,但是当Class属性 myEmail.Bcc.Email <时/ strong>没有值传递我得到“对象引用没有设置为对象的实例”错误。

我可以修改构造函数以避免这种情况吗?

感谢。

电子邮件消息类

Public Class EmailMessage

    Public Sub New()

        Dim ToAddress As New EmailAddress
        Dim FromAddress As New EmailAddress
        Dim Bcc As New EmailAddress
    End Sub

    Public Property ToAddress() As EmailAddress
        Get
            Return m_To
        End Get
        Set(value As EmailAddress)
            m_To = value
        End Set
    End Property
    Private m_To As EmailAddress

    Public Property FromAddress() As EmailAddress
        Get
            Return m_From
        End Get
        Set(value As EmailAddress)
            m_From = value
        End Set
    End Property
    Private m_From As EmailAddress

    Public Property Bcc() As EmailAddress
        Get
            Return m_Bcc
        End Get
        Set(value As EmailAddress)
            m_Bcc = value
        End Set
    End Property
    Private m_Bcc As EmailAddress

    Public Property Subject() As String
        Get
            Return m_Subject
        End Get
        Set(value As String)
            m_Subject = value
        End Set
    End Property
    Private m_Subject As String

    Public Property Body() As String
        Get
            Return m_Body
        End Get
        Set(value As String)
            m_Body = value
        End Set
    End Property
    Private m_Body As String

End Class

电子邮件地址类

Public Class EmailAddress

    Public Sub New()
        Dim Email As String = ""
        Dim Name As String = ""
    End Sub

    Public Property Email() As String
        Get
            Return m_Email
        End Get
        Set(value As String)
            m_Email = value
        End Set
    End Property
    Private m_Email As String

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(value As String)
            m_Name = value
        End Set
    End Property
    Private m_Name As String
End Class

代码

    Public Sub AssignVariables()

        Dim myEmail As New EmailMessage
        Dim toAddress As New EmailAddress
        Dim fromAddress As New EmailAddress

        toAddress.Email = "example@example.com"
        fromAddress.Email = "example@example.com"

        myEmail.ToAddress = toAddress
        myEmail.FromAddress = fromAddress
        myEmail.Subject = "Example"
        myEmail.Body = "Example"

        Call SendEmail(myEmail)
    End Sub

    Public Sub SendEmail(ByVal email As EmailMessage)

        Send SendEmail(email.FromAddress.Email, email.ToAddress.Name, email.ToAddress.Email, email.Bcc.Email, email.Subject, email.Body)
    End Sub


    Private Sub SendEmail(fromAddress As String, toName As String, toAddress As String, bcc As String, subject As String, body As String)

    End Sub

1 个答案:

答案 0 :(得分:2)

属性应定义为ReadOnly。如果提供了non,则在构造函数中创建每个属性的新实例。像这样:

Public Class EmailMessage

    Public Sub New()
        Me.New(Nothing, Nothing, Nothing)
    End Sub

    Public Sub New([to] As EmailAddress, [from] As EmailAddress, bcc As EmailAddress)
        Me.m_To = If(([to] Is Nothing), New EmailAddress, [to])
        Me.m_From = If(([from] Is Nothing), New EmailAddress, [from])
        Me.m_Bcc = If((bcc Is Nothing), New EmailAddress, bcc)
    End Sub

    Public ReadOnly Property ToAddress() As EmailAddress
        Get
            Return m_To
        End Get
    End Property

    Public ReadOnly Property FromAddress() As EmailAddress
        Get
            Return m_From
        End Get
    End Property

    Public ReadOnly Property Bcc() As EmailAddress
        Get
            Return m_Bcc
        End Get
    End Property

    Public Property Subject() As String
        Get
            Return m_Subject
        End Get
        Set(value As String)
            m_Subject = value
        End Set
    End Property

    Public Property Body() As String
        Get
            Return m_Body
        End Get
        Set(value As String)
            m_Body = value
        End Set
    End Property

    Private ReadOnly m_To As EmailAddress
    Private ReadOnly m_From As EmailAddress
    Private ReadOnly m_Bcc As EmailAddress
    Private m_Body As String
    Private m_Subject As String 

End Class