我知道这是一个相对常见的错误,但是我正在应用程序中工作,该应用程序允许使用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设置子弦中的字符数时,就会发生错误。如果我用一个数字替换它,则报告将正确发布。

有帮助吗?

解决方案

您可能想使用 string.split 这将使这容易得多

例如

   If Not String.IsNullOrWhiteSpace(lastNameFirstName) Then

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

    End If

如果您没有.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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top