|
Public Class City
Private _Name As String
Private _Inhabitants As Integer
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Inhabitants As Integer)
_Name = Name
_Inhabitants = Inhabitants
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public Property Inhabitants() As Integer
Get
Return _Inhabitants
End Get
Set(ByVal NewValue As Integer)
_Inhabitants = NewValue
End Set
End Property
End Class
Public Class frmObjectDatabinding
Private Sub frmObjectDatabinding_Load(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles MyBase.Load
Const MaxCount As Integer = 1000000
Dim Rnd As New Random With New State("Germany")
With .Cities
.Add(New City("Hamburg", Rnd.Next(1, MaxCount)))
.Add(New City("Stuttgart", Rnd.Next(1, MaxCount)))
.Add(New City("Berlin", Rnd.Next(1, MaxCount)))
.Add(New City("Bonn", Rnd.Next(1, MaxCount)))
End With
.AddTo(Me.StateBindingSource)
End With
With New State("France")
With .Cities
.Add(New City("Paris", Rnd.Next(1, MaxCount)))
.Add(New City("Orleans", Rnd.Next(1, MaxCount)))
.Add(New City("Nancy", Rnd.Next(1, MaxCount)))
.Add(New City("Strasbourg", Rnd.Next(1, MaxCount)))
End With
.AddTo(Me.StateBindingSource)
End With
With New State("England")
With .Cities
.Add(New City("London", Rnd.Next(1, MaxCount)))
.Add(New City("Bristol", Rnd.Next(1, MaxCount)))
.Add(New City("Cambridge", Rnd.Next(1, MaxCount)))
.Add(New City("Hampshire", Rnd.Next(1, MaxCount)))
End With
.AddTo(Me.StateBindingSource)
End With
End Sub
End Class
Public Class State
Private _Cities As New List(Of City)
Private _Name As String
Public Sub New()
End Sub
Public Sub New(ByVal Name As String)
_Name = Name
End Sub
Public ReadOnly Property Cities() As List(Of City)
Get
Return _Cities
End Get
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Public ReadOnly Property Inhabitants() As Integer
Get
For Each C As City In _Cities
Inhabitants += C.Inhabitants
Next
End Get
End Property
Public Sub AddTo(ByVal Src As System.ComponentModel.IBindingList)
Src.Add(Me)
End Sub
End Class |