我试图在这一行中重新训练一行类:
For i As Integer = 0 To RacerList(i).CompatibilityArr.Count - 1
For o As Integer = 0 To RacerList(i).CompatibilityArr.Count - 1
ReDim Preserve RacerList(i).CompatibilityArr(o).HeightCompArr(AmountOfRacers - 1)
Next
Next
Redim代码行导致"对象引用未设置为对象的实例。"
在这行代码之上,我得到了:
For i As Integer = 0 To RacerList.Count - 1
ReDim Preserve RacerList(i).CompatibilityArr(AmountOfRacers - 1)
Next
哪种方法非常好,所以我很确定错误在HeightCompArr中(同样,当我写" HeightCompArr。"它不建议在下拉菜单中的儿童内容)
以下是单独模块形式的所有类/结构/数组:
Public RacerList As New List(Of Racer)
<Serializable()> Public Class Racer
Public Property Name As String
Public Property CleatSize As String
Public Property SkillLevel As String
Public Property Height As String
Public Property Team As String
Public CompatibilityArr() As Compatibility
End Class
<Serializable()> Public Class Compatibility
Public HeightCompArr() As HeightComp
End Class
<Serializable()> Public Class HeightComp
Public Name As String
Public Score As Integer
End Class
我不确定这是一个问题,我是如何在数组中包含数组或者是什么。我非常感谢任何建议。
答案 0 :(得分:1)
您有语法错误和设计问题。
正如其他人所指出的那样,语法问题是REDIM PRESERVE只为对象创造空间,它实际上并不创建对象。要解决此问题,您需要为数组的每个元素创建一个新对象。
您遇到的设计问题是您尝试将每个赛车手与其他赛车手进行比较,但您将比较存储在Racer类中。 Racer引用应该在Comparison中,而不是相反。
我建议您使用List(Of ...)
代替数组并重新构建如下:
Private Sub Main()
Dim RacerList As New List(Of Racer)
Dim CompatibilityArr As New List(Of Compatibility)
Dim Adam As New Racer With {.Name = "Adam", .CleatSize = "10", .SkillLevel = "2", .Height = "180", .Team = "Blue"}
Dim Bill As New Racer With {.Name = "Bill", .CleatSize = "11", .SkillLevel = "3", .Height = "185", .Team = "Blue"}
Dim Charlie As New Racer With {.Name = "Charlie", .CleatSize = "12", .SkillLevel = "4", .Height = "190", .Team = "Red"}
RacerList.Add(Adam)
RacerList.Add(Bill)
RacerList.Add(Charlie)
CompatibilityArr.Add(New Compatibility With {.Racer1 = Adam, .Racer2 = Bill, .HeightCompScore = 5})
CompatibilityArr.Add(New Compatibility With {.Racer1 = Adam, .Racer2 = Charlie, .HeightCompScore = 6})
CompatibilityArr.Add(New Compatibility With {.Racer1 = Bill, .Racer2 = Charlie, .HeightCompScore = 7})
End Sub
<Serializable()> Public Class Racer
Public Property Name As String
Public Property CleatSize As String
Public Property SkillLevel As String
Public Property Height As String
Public Property Team As String
End Class
<Serializable()> Public Class Compatibility
Public Racer1 As Racer
Public Racer2 As Racer
Public HeightCompScore As Integer
End Class