我不熟悉结构的类型或我需要用来实现这一点的任何东西,但我知道有一个。
我正努力做到这一点,以便我可以参考类似的东西:
Resolve
而不是我现在要做的事情:
RegisterInstance
基本上,我想在compatibilityarr中添加项目(可能需要更改为一个很好的列表),并且能够将racer引用为自己的名称,而不是使用索引。
答案 0 :(得分:0)
这是构建符合您需求的解决方案的一种方法,如上所述。它需要一个构建为List(Of T)
的嵌入式类,我们重载该属性以接受字符串而不是整数。
Public Class Foo
Public Property compatibilityArr As New Members
End Class
Public Class Members : Inherits List(Of Member)
Public Overloads ReadOnly Property Item(name As String) As Member
Get
Return Me.Where(Function(i) i.Name = name).FirstOrDefault
End Get
End Property
End Class
Public Class Member
Public Property Name As String
Public Property CleatScore As Integer
End Class
然后使用它:
Public Class Form1
Dim f As New Foo
Private Sub loads() Handles Me.Load
Dim member As New Member With {.Name = "John", .CleatScore = 10}
f.compatibilityArr.Add(member)
MessageBox.Show(f.compatibilityArr.Item("John").CleatScore)
End Sub
End Class
答案 1 :(得分:0)
还有其他方法可以做到这一点,但最简单的方法是编写一个函数来按名称搜索数组:
Sub Main1()
Dim racerlist(2) As Racer
racerlist(0) = New Racer With {.Name = "Adam", .CleatScore = "1"}
racerlist(1) = New Racer With {.Name = "Bill", .CleatScore = "2"}
racerlist(2) = New Racer With {.Name = "Charlie", .CleatScore = "3"}
For i As Integer = 0 To racerlist.GetUpperBound(0)
For j As Integer = 0 To racerlist.GetUpperBound(0)
If racerlist(j).Name <> racerlist(i).Name Then
ReDim Preserve racerlist(i).CompatibilityArr(racerlist(i).CompatibilityArr.GetUpperBound(0) + 1)
racerlist(i).CompatibilityArr(racerlist(i).CompatibilityArr.GetUpperBound(0)) = racerlist(j)
End If
Next j
Next i
Dim racerBill As Racer = Racer.FindRacer(racerlist, "Bill")
MsgBox(racerBill.FindCompatibility("Charlie").CleatScore)
End Sub
Class Racer
Property Name As String
Property CleatScore As String
Property CompatibilityArr As Racer()
Sub New()
ReDim CompatibilityArr(-1) 'initialise the array
End Sub
Function FindCompatibility(name As String) As Racer
Return FindRacer(CompatibilityArr, name)
End Function
Shared Function FindRacer(racerlist() As Racer, name As String) As Racer
For i As Integer = 0 To racerlist.GetUpperBound(0)
If racerlist(i).Name = name Then
Return racerlist(i)
End If
Next i
Return Nothing
End Function
End Class
答案 2 :(得分:0)
正如@Codexer所提到的,我使用字典来实现这一点。
在我的Racers列表(RacerList)中,我有RacerCompatibility,我创建的类似于下面的内容:
Public RacerCompatibility As New Dictionary(Of String, Compatibility)
创建兼容性,如:
Public Class Compatibility
Public Cleat As Boolean
Public Skill As Integer
Public Height As Integer
End Class
所以现在我可以访问列表中的racer兼容性,如:
RacerList(x)的.RacerCompatibility.Item( “约翰”)