将单选按钮参数传递给vb.net中的另一个函数

时间:2011-10-31 17:47:05

标签: vb.net function radio-button parameter-passing

我有2个函数,一个是SaveData(),另一个是SendEmail()

在SaveData()中

保存选中的单选按钮,最后调用SendMail()。

在SendMail()中,单选按钮被“测试”,然后根据无线电选择发送两封电子邮件中的一封。

我遇到的问题是正确传递参数。我尝试了几种不同的方法,但参数没有发送到SendMail()

单选按钮是是/否问题

有关为什么没有传递参数的任何想法?

这就是我所拥有的:

Protected Function SaveData()
...
Q1 = Me.rblnewqst1.SelectedValue
        Q2 = Me.rblnewqst2.SelectedValue
        Q3 = Me.rblnewqst3.SelectedValue
        Q4 = Me.rblnewqst4.SelectedValue
        Q5 = Me.rblnewqst5.SelectedValue

...

和SendEmail()是

Protected Sub SendEmail(ByVal rblnewqst1 As Object, ByVal rblnewqst2 As Object, ByVal rblnewqst3 As Object, ByVal rblnewqst4 As Object, ByVal rblnewqst5 As Object)

    If rblnewqst1.SelectedValue = 2 Or rblnewqst2.SelectedValue = 2 Or rblnewqst3.SelectedValue = 2 Or rblnewqst4.SelectedValue = 2 Or rblnewqst5.SelectedValue = 2 Then

        Dim sResponseFromName As String = "ex@example.com"
        Dim sResponseToName As String = txtEmail.Text
        Dim sResponseSubject As String = "Denied"
        Dim sResponseBody As String = "Message>"


        Try
            Dim mm As New MailMessage(sResponseFromName, sResponseToName)
            Dim SMTP As New SmtpClient

            SMTP.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis

            mm.Subject = sResponseSubject
            mm.Body = sResponseBody
            mm.IsBodyHtml = True

            Try

                SMTP.Send(mm)

            Catch exSmtpException As SmtpException
                Dim stemp As String = exSmtpException.Message.ToString
            End Try

        Catch ex As ApplicationException
            Dim stemp As String = ex.InnerException.Message.ToString
        End Try

    Else
        Dim sResponseFromName As String = "ex@example.com"
        Dim sResponseToName As String = txtEmail.Text
        Dim sResponseSubject As String = "Accepted"
        Dim sResponseBody As String = "MESSAGE....."

        Try
            Dim mm As New MailMessage(sResponseFromName, sResponseToName)
            Dim SMTP As New SmtpClient

            SMTP.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis

            mm.Subject = sResponseSubject
            mm.Body = sResponseBody
            mm.IsBodyHtml = True

            Try

                SMTP.Send(mm)

            Catch exSmtpException As SmtpException
                Dim stemp As String = exSmtpException.Message.ToString
            End Try

        Catch ex As ApplicationException
            Dim stemp As String = ex.InnerException.Message.ToString
        End Try
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

与他在评论中提到的HardCode一样,我建议您始终使用Option Explicit和Option Strict。

我要做的第一件事就是改变SendMail部分的工作方式,从单选按钮中取值。你的If是检查和整数值,所以我会使用Integer作为类型。

更像是:

Protected Sub SendEmail(ByVal q1 As Integer, ByVal q2 As Integer)

If q1 = 2 Or q2 = 2 Then
etc...
End IF

我缩短了列表,所以我不必输入那么多。一般来说,我认为发送值比整个对象更好,除非您因特定原因需要整个控件。此外,除非您别无选择,否则发送对象不是我建议做的事情。

我建议,测试它的另一件事是在sendEmail Sub中设置一个断点,使用快速监视来查看发送到您正在检查的方法的实际对象/类型。