私はいくつかの読書をしましたが、私のVB2010プロジェクトで(クラスの)リストを複製するための最良のアプローチが何であるかについて頭を悩ませているようです。私はそのように関連している3つのクラスを持っています
Public Class City
'here are many fields of type string and integer
Public Roads As New List(Of Road)
End Class
Public Class Road
'here are many fields of type string and integer
Public Hazards As New List(Of Hazard)
End Class
Public Class Hazard
Implements ICloneable
'here are many fields of type string and integer and double
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
私が取り組んでいる都市があるとしましょう。ベースとして 1 つの道路とその危険を作成し、次に別の道路を追加しますが、前の道路の危険を出発点として使用し、フィールドを微調整したい場合があります。 .
Dim rd As New Road
'add road fields
dim hz1 as New Hazard
'add hazard fields
dim hz2 as New Hazard
'add hazard fields
'add the hazard objects to the road
rd.Hazards.Add(hz1)
rd.Hazards.Add(hz2)
'add the road to the city
myCity.Roads.Add(rd)
'here I want to start a new road based on the old road
Dim rdNew As New Road
'copy or clone the hazards from old road
rdNew.Hazards = rd.Hazards '<============
'over-write some of the hazard fields
rdNew.Hazards(0).Description = "temp"
したがって、クラスをコピーすると、コンテンツではなくポインターがコピーされることがわかります。ハザード クラスで ICloneable インターフェイスを使用しましたが、正しく使用しているとは言えません。Hazards 変数は、Hazard クラスのリストです。そのクラスのクローンを作成するにはどうすればよいですか?