【发布时间】:2019-01-22 19:58:52
【问题描述】:
我有一个对象列表,其中每个对象都包含一个字符串属性。这些字符串属性必须是唯一的,所以我尝试将升序数字添加到重复项。首先,我认为this 可以帮助我,但它适用于列表而不是对象列表,并且所有将代码转换为 vb 的尝试都没有成功,所以我尝试了另一种方法。
Object1.String = "Test"
Object2.String = "Test"
应该转化为
Object1.String = "Test"
Object2.String = "Test1"
这是我尝试过的:
Module Module1
Sub Main()
Dim obj1 As New TestCls
Dim obj2 As New TestCls
Dim obj3 As New TestCls
obj1.p1 = "Test"
obj2.p1 = "Test1"
obj3.p1 = "Test"
Dim lstmp As New List(Of TestCls)
lstmp.Add(obj1)
lstmp.Add(obj2)
lstmp.Add(obj3)
For Each elementA As TestCls In lstmp
For Each elementB As TestCls In lstmp
If elementA.p1 = elementB.p1 Then
elementB.p1 = elementB.p1 & 1
End If
Next
Next
Dim i As Integer = 0
End Sub
End Module
Class TestCls
Public Property p1 As String
End Class
但结果是 Test1 和 Test111 并且与很多对象更加混淆。有谁能帮帮我吗?
【问题讨论】:
标签: vb.net list duplicates rename