我有3个课程。比赛,赛跑者和RaceRunner。 RaceRunner继承了Runner。
我有3个清单。 RunnerList,RaceList和raceRunners。 raceRunners存储在Race类中。
我有一个循环如下:
For Each Runner In Runnerlist
If uiaddraceRunID.Text = Runner.UniqueRunnerID Then
For Each Race In RaceList
If uiaddrunnerracelist.SelectedItem = Race.raceDate Then
Dim rRunner As New raceRunner
rRunner = Runner
Race.raceRunners.Add(rRunner)
End If
Next
End If
Next
循环遍历跑步者列表中的跑步者,如果跑步者ID与表格上提供的跑者ID匹配,则循环播放Racelist中的种族。如果列表框中选定的Race与Race的Date匹配,则它会创建raceRunner类的新对象(继承Runner)。我希望这会将选定的Runners信息复制到自己的信息中,然后将其添加到Race类的raceRunners列表中。
这可能吗?
编辑:
根据建议,我已将循环更改为:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each Runner In Runnerlist
Dim runner1 = Runner
If uiaddraceRunID.Text = Runner.UniqueRunnerID Then
For Each Race In RaceList
If uiaddrunnerracelist.SelectedItem = Race.raceDate Then
Dim runner2 As raceRunner = DirectCast(runner1.ShallowCopy(), Runner)
End If
Next
End If
Next
End Sub
我在Runner类中编写了这样的函数
Public Function ShallowCopy() As Runner
Return DirectCast(Me.MemberwiseClone(), Runner)
End Function
但我不能将Runner类型的对象克隆到TypeRonner类型的对象。这有可能吗?