Vb.net 如何创建布尔函数来列出泛型集合中的对象?

Vb.net 如何创建布尔函数来列出泛型集合中的对象?,vb.net,Vb.net,在创建一个列表(共T个)之后,我想创建一个布尔函数。首先,我们将要求data在列表中添加一个对象。但是,如果这个新对象具有相同的“DNI”(类Aspirante的String属性),那么我们不能将这个新对象包含在列表中。因此,当我们有一个具有相同属性的对象时,它应该是True,当我们没有属性时,它应该是False,这样我们就可以添加新对象了 下面是我所做的代码: Public Class Oposicion Private datos As New List(Of Aspira

在创建一个
列表(共T个)
之后,我想创建一个
布尔函数。首先,我们将要求data在列表中添加一个对象。但是,如果这个新对象具有相同的“DNI”(类Aspirante的String属性),那么我们不能将这个新对象包含在列表中。因此,当我们有一个具有相同属性的对象时,它应该是
True
,当我们没有属性时,它应该是
False
,这样我们就可以添加新对象了

下面是我所做的代码:

Public Class Oposicion

        Private datos As New List(Of Aspirante)()
     

        Public Function Alta(ByRef objAspirante As Aspirante) As Boolean


            If datos.Contains(objAspirante.DNI) Then
               

                Return True

            Else

                datos.Add(objAspirante)

                Return False
            End If
        End Function



End Class

然而,它不起作用。我不知道怎么做。如果我不够清楚,很抱歉。

您可以创建一个
列表(字符串)
,其中只包含
datos中每个对象的DNI属性。然后查看
objAspirante
的DNI是否包含在
lst

Public Class Oposicion
    Private datos As New List(Of Aspirante)()

    Public Function Alta(objAspirante As Aspirante) As Boolean
        Dim lst As New List(Of String)
        For Each a As Aspirante In datos
            lst.Add(a.DNI)
        Next
        If lst.Contains(objAspirante.DNI) Then
            Return True
        Else
            datos.Add(objAspirante)
            Return False
        End If
    End Function

End Class
如果可以更改DATO的类型,这可能会更容易

Public Class Oposicion
    Private datos As New Dictionary(Of String, Aspirante)()

    Public Function Alta(objAspirante As Aspirante) As Boolean
        If datos.ContainsKey(objAspirante.DNI) Then
            Return True
        Else
            datos.Add(objAspirante.DNI, objAspirante)
            Return False
        End If
    End Function

End Class

您可以创建一个
列表(字符串)
,其中只包含
datos
中每个对象的DNI属性。然后查看
objAspirante
的DNI是否包含在
lst

Public Class Oposicion
    Private datos As New List(Of Aspirante)()

    Public Function Alta(objAspirante As Aspirante) As Boolean
        Dim lst As New List(Of String)
        For Each a As Aspirante In datos
            lst.Add(a.DNI)
        Next
        If lst.Contains(objAspirante.DNI) Then
            Return True
        Else
            datos.Add(objAspirante)
            Return False
        End If
    End Function

End Class
如果可以更改DATO的类型,这可能会更容易

Public Class Oposicion
    Private datos As New Dictionary(Of String, Aspirante)()

    Public Function Alta(objAspirante As Aspirante) As Boolean
        If datos.ContainsKey(objAspirante.DNI) Then
            Return True
        Else
            datos.Add(objAspirante.DNI, objAspirante)
            Return False
        End If
    End Function

End Class

这不会直接回答您的问题,但它涉及大量代码,因此在注释中不起作用

首先,您可能不应该使用
列表(t)
HashSet(Of T)
已经包含了防止添加重复项的功能,因此这可能是一个更好的选择。如果要比较特定属性值上的对象,则需要首先基于该属性值创建比较器:

公共类的东西
作为字符串的公共属性
末级
公共类内容比较器
实现IEqualityComparer(事物的)
公共重载函数Equals(x为Thing,y为Thing)作为布尔实现IEqualityComparer(Of Thing)。Equals
返回x.Stuff.Equals(y.Stuff)
端函数
公共重载函数GetHashCode(obj作为Thing)作为整数实现IEqualityComparer(Of Thing).GetHashCode
返回obj.GetHashCode()
端函数
末级
然后创建一个
HashSet(Of T)
,它使用该比较器来确定相等性:

Dim things As New HashSet(Of Thing)(New ThingComparer)
然后,您可以通过调用
add
随意添加项目。这将添加新项目并返回
True
,或者不会添加重复项目并返回
False

Dim variousStuff={“一”、“二”、“一”}
对于各种各样的东西
用{.Stuff=Stuff}将某物暗显为新事物
如果是,那么加上(某物)
Console.WriteLine($“{stuff}”已成功添加。”)
其他的
WriteLine($“{stuff}”是重复的,未添加。“)
如果结束
下一个

潜在的缺点是
HasSet(Of T)
未实现
IList(Of T)
,因此无法按索引访问项目。不过,它确实实现了
ICollection(OF T)
,因此它确实有一个
Count
属性,您可以为每个
循环使用
枚举它。

这并不能直接回答您的问题,但它涉及大量代码,因此它在注释中不起作用

首先,您可能不应该使用
列表(t)
HashSet(Of T)
已经包含了防止添加重复项的功能,因此这可能是一个更好的选择。如果要比较特定属性值上的对象,则需要首先基于该属性值创建比较器:

公共类的东西
作为字符串的公共属性
末级
公共类内容比较器
实现IEqualityComparer(事物的)
公共重载函数Equals(x为Thing,y为Thing)作为布尔实现IEqualityComparer(Of Thing)。Equals
返回x.Stuff.Equals(y.Stuff)
端函数
公共重载函数GetHashCode(obj作为Thing)作为整数实现IEqualityComparer(Of Thing).GetHashCode
返回obj.GetHashCode()
端函数
末级
然后创建一个
HashSet(Of T)
,它使用该比较器来确定相等性:

Dim things As New HashSet(Of Thing)(New ThingComparer)
然后,您可以通过调用
add
随意添加项目。这将添加新项目并返回
True
,或者不会添加重复项目并返回
False

Dim variousStuff={“一”、“二”、“一”}
对于各种各样的东西
用{.Stuff=Stuff}将某物暗显为新事物
如果是,那么加上(某物)
Console.WriteLine($“{stuff}”已成功添加。”)
其他的
WriteLine($“{stuff}”是重复的,未添加。“)
如果结束
下一个

潜在的缺点是
HasSet(Of T)
未实现
IList(Of T)
,因此无法按索引访问项目。它确实实现了
ICollection(OF T)
,因此它确实有一个
Count
属性,您可以用
为每个
循环枚举它。

如果您想继续使用现有的
列表(OF Aspirante)
,那么只需使用
.Any()
并向它传递一个Lambda来确定是否已经存在。看起来是这样的:

Public Function Alta(ByVal objAspirante As Aspirante) As Boolean
    If Not datos.Any(Function(x) x.DNI = objAspirante.DNI) Then
        datos.Add(objAspirante)
        Return True ' your description and code did not match for these!
    End If
    Return False ' your description and code did not match for these!
End Function

请注意我在
返回
行上的评论…您的代码和描述在此不匹配。

如果您想继续使用现有的
列表(Aspirante)
,只需使用
.Any()
并向其传递Lambda以确定是否已经存在。看起来是这样的:

Public Function Alta(ByVal objAspirante As Aspirante) As Boolean
    If Not datos.Any(Function(x) x.DNI = objAspirante.DNI) Then
        datos.Add(objAspirante)
        Return True ' your description and code did not match for these!
    End If
    Return False ' your description and code did not match for these!
End Function

请注意我在
返回
行上的评论…此处您的代码和描述不匹配。

你好,朱伦。您的代码显然不是VBA代码。VBA和VB.NET有相似之处,但不是相同的语言。为了对鸭子的热爱,为什么要使用“ByRef”?!那应该是“拜瓦尔”。通过引用传递意味着你