2011-01-21 127 views
0

我知道這是一個相對常見的錯誤,但我正在一個應用程序中允許使用vb.net或C#編寫自定義報告進行腳本編寫。錯誤處理非常差,我不知道如何添加我自己的(如果它甚至可能)。異常已被調用的目標引發

我的代碼只是檢索存儲在格式爲的報告上的文本框中的值,名字並截斷了逗號後的所有字符。此值姓氏被放置在報告上的新文本框中。這是我的代碼:

Sub Detail1_Format 

    Dim lastNameFirstName As String = "" 
    Dim lastName As String = "" 
    Dim lastNameCommaIndex As Integer= 

'set lastNameFirstName value from data on report' 
    lastNameFirstName = ReportUtilities.ReturnTextBoxValue(rpt,"Detail1","txtdtr_DTOOLS_CompanyName") 

'find index of first comma in lastNameFirstName string' 
    lastNameCommaIndex = lastNameFirstName.IndexOf(",") 

'set contents of lastName using substring of lastNameFirstString' 
    lastName = lastNameFirstName.SubString(0, lastNameCommaIndex) 
'the error happens above when I use lastNameCommaIndex' 
'to set the number of characters in my substring' 

'set client name textbox value using lastName' 
    ReportUtilities.SetTextBoxValue(rpt,"Detail1","txtdtr_CALC_ClientName",lastName) 

End Sub 

當我使用lastNameCommaIndex來設置我的子字符串中的字符數時會發生錯誤。如果我用一個數字替換它,報告就會正確發佈。

回答

0

您可能需要使用string.split它將使這個輕鬆了許多

例如

If Not String.IsNullOrWhiteSpace(lastNameFirstName) Then 

     lastName = lastNameFirstName.Split(",".ToCharArray())(0) 

    End If 

這裏是你如何寫IsNullOrWriteSpace如果你沒有.NET 4.0

Function IsNullOrWhiteSpace(ByVal s As String) As Boolean 

    If s Is Nothing Then 
     Return True 
    End If 

    Return s.Trim() = String.Empty 

End Function 
+0

OK太好了!我完全按照您的顯示進行了嘗試,並且收到有關.IsNullOrWhiteSpace()不是String成員的錯誤。我認爲這可能是應用程序中.NET支持的未知版本的限制。所以我只是在If語句的上下文之外使用了.split代碼,因爲總會有一個值。完美的作品。謝謝! – fryeguy 2011-01-21 22:27:39

相關問題