index超出了数组vb.net的范围

时间:2017-08-10 15:02:04

标签: asp.net vb.net

Private Sub GetGeneralMemberInformation(Member As WebServices.MemberInfoService.Member)
        Try
            Dim error_msg As String = "The following Member is not Eligible to display the  data: {0} "
            SubscriberId = Member.SubscriberId
            If Member.Eligibility(0) Is Nothing Then
                Throw New Exception(String.Format(error_msg, SubscriberId))
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

我无法检查对Eligibility对象执行null检查。在检查自身时我得到的错误就像索引一样超出了数组的范围。任何机构都可以建议我如何进行空检查,我想显示mu自定义错误信息而不是那个,这里资格对象没有任何数据。

1 个答案:

答案 0 :(得分:2)

正如其他人所说,资格可能是空的。既然您澄清了Eligibility是List(Of Object),我更新了下面的代码示例以反映它。

Imports System
Imports System.Collections.Generic

Public Module Module1
    Public Sub Main()
        Dim member1 = New Member()
        member1.Eligibilities = New List(Of MemberEligibility)

        ' REMARKS: List is empty
        If member1.Eligibilities.Count > 0 Then
            If member1.Eligibilities(0) Is Nothing Then
                Console.WriteLine("First element is NULL")
            End If
        End If

        ' REMARKS: Add one element to list
        member1.Eligibilities.Add(New MemberEligibility())

        ' REMARKS: List contains one element        
        If member1.Eligibilities.Count > 0 Then
            If member1.Eligibilities(0) IsNot Nothing Then
                Console.WriteLine("First element is initialized")
            End If
        End If

    End Sub
End Module

Public Class Member
    Private _eligibility As MemberEligibility
    Private _eligibilities As List(Of MemberEligibility)

    Public Sub New()
    End Sub

    Public Property Eligibility As MemberEligibility
        Get
            Return Me._eligibility
        End Get

        Set
            Me._eligibility = Value
        End Set
    End Property

    Public Property Eligibilities As List(Of MemberEligibility)
        Get
            Return Me._eligibilities
        End Get

        Set
            Me._eligibilities = Value
        End Set
    End Property
End Class

Public Class MemberEligibility
    Public Sub New()
    End Sub
End Class

以下是显示上述代码的.NET Fiddle