2011-10-31 126 views
0

我有2個函數,一個是保存數據(),而另一個是SendEmail()傳遞單選按鈕參數給另一個函數在vb.net

在保存數據()被選擇保存在單選按鈕,和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 = "[email protected]" 
     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 = "[email protected]" 
     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 
+0

什麼是變量Q1,Q2等聲明爲?如果我是你,我會打開Option Explicit和Option Strict,然後看看你的數據類型轉換問題(如果有的話)。 – HardCode

+1

這不能強調,始終與選項嚴格和顯式打開。起初它可能有點痛苦,但它會讓你成爲一個更好的程序員。 –

回答

1

像硬編碼在他的評論中提到的,我建議你總是使用Option Explicit和Option Strict on。

我會做的第一件事是改變發送郵件部分的工作方式,從單選按鈕中獲取值。你的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中設置一個斷點,使用快速監視來查看正在發送到正在檢查的方法的實際對象/類型。

相關問題