'Visual Basic 2008/2010 Containment Object Oriented Programming Class Person Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Private m_Address As Address Public Property Address() As Address Get Address = m_Address End Get Set(ByVal value As Address) m_Address = value End Set End Property End Class Class Address Private m_Street As String Public Property Street() As String Get Street = m_Street End Get Set(ByVal value As String) m_Street = value End Set End Property Private m_Number As String Public Property Number() As String Get Number = m_Number End Get Set(ByVal value As String) m_Number = value End Set End Property Private m_ZipCode As String Public Property ZipCode() As String Get ZipCode = m_ZipCode End Get Set(ByVal value As String) m_ZipCode = value End Set End Property Private m_City As String Public Property City() As String Get City = m_City End Get Set(ByVal value As String) m_City = value End Set End Property End Class Module Client1 Sub Main() Dim address1 As Address = New Address address1.Street = "Royal Avenue" address1.Number = "10" address1.ZipCode = "90210" address1.City = "Beverly Hills" ' Dim person1 As Person = New Person person1.Name = "John" person1.Address = address1 ' Print(person1) ' Console.ReadLine() End Sub Sub Print(ByVal person As Person) Console.WriteLine(person.Name) If person.Address IsNot Nothing Then With person.Address Console.WriteLine(.Number & ", " & .Street) Console.WriteLine(.City & " " & .ZipCode) End With End If End Sub End Module Module Client2 Sub Main() Dim person1 As Person = New Person person1.Name = "John" person1.Address = New Address person1.Address.Street = "Royal Avenue" ' (1) person1.Address.Number = "10" person1.Address.ZipCode = "90210" person1.Address.City = "Beverly Hills" Print(person1) ' Dim person2 As Person = New Person person2.Name = "Jane" person2.Address = person1.Address ' (2) Print(person2) ' Console.ReadLine() End Sub End Module Class Customer Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Private m_Address As Address = New Address ' (1) Public Property Address() As Address Get Address = m_Address End Get Set(ByVal value As Address) m_Address = value End Set End Property End Class Module Client3 Sub Main() Dim customer1 As Customer = New Customer customer1.Address.City = "New York" ' Console.WriteLine(customer1.Address.City) ' Console.ReadLine() End Sub End Module Class Position Private m_X As Integer Public Property X() As Integer Get X = m_X End Get Set(ByVal value As Integer) m_X = value End Set End Property Private m_Y As Integer Public Property Y() As Integer Get Y = m_Y End Get Set(ByVal value As Integer) m_Y = value End Set End Property End Class Module PositionTextFixture Sub Main() Dim position1 As Position = New Position Console.WriteLine(position1.X = 0) Console.WriteLine(position1.Y = 0) ' position1.X = 5 Console.WriteLine(position1.X = 5) Console.WriteLine(position1.Y = 0) ' position1.Y = 10 Console.WriteLine(position1.X = 5) Console.WriteLine(position1.Y = 10) ' Console.ReadLine() End Sub End Module Class Robot Private m_Position As Position = New Position Public ReadOnly Property Position() As Position Get Position = m_Position End Get End Property Private m_Direction As Integer Public Sub Rotate() m_Direction += 1 If m_Direction > 3 Then m_Direction = 0 End Sub Public Sub PlaceStep() Select Case m_Direction Case 0 ' up Position.Y = Position.Y + 1 Case 1 ' right Position.X = Position.X + 1 Case 2 ' down Position.Y = Position.Y - 1 Case 3 ' left Position.X = Position.X - 1 End Select End Sub End Class Module RobotTestFixture Public Sub Main() Dim robot1 As Robot = New Robot Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 0) ' robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 1) ' robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 2) ' robot1.Rotate() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 2) ' robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 1) Console.WriteLine(robot1.Position.Y = 2) ' robot1.Rotate() robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 1) Console.WriteLine(robot1.Position.Y = 1) ' robot1.Rotate() robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 1) ' robot1.Rotate() robot1.PlaceStep() Console.WriteLine(robot1.Position IsNot Nothing) Console.WriteLine(robot1.Position.X = 0) Console.WriteLine(robot1.Position.Y = 2) ' Console.ReadLine() End Sub End Module Module PointAndLineTestFixtures Sub Main() PointTestFixture() LineTextFixture() ' Console.ReadLine() End Sub Sub PointTestFixture() Dim point1 As Point = New Point Console.WriteLine(point1.X = 0) Console.WriteLine(point1.Y = 0) ' point1.X = 1 Console.WriteLine(point1.X = 1) Console.WriteLine(point1.Y = 0) ' point1.Y = 3 Console.WriteLine(point1.X = 1) Console.WriteLine(point1.Y = 3) End Sub Sub LineTextFixture() Dim line1 As Line = New Line Console.WriteLine(line1.StartPoint Is Nothing) Console.WriteLine(line1.EndPoint Is Nothing) Console.WriteLine(line1.Length = 0) ' Dim startPoint As Point = New Point With {.X = 1, .Y = 4} line1.StartPoint = startPoint Console.WriteLine(line1.StartPoint Is startPoint) Console.WriteLine(line1.EndPoint Is Nothing) Console.WriteLine(line1.Length = 0) ' Dim endPoint As Point = New Point With {.X = 5, .Y = 1} line1.EndPoint = endPoint Console.WriteLine(line1.StartPoint Is startPoint) Console.WriteLine(line1.EndPoint Is endPoint) Console.WriteLine(line1.Length = 5) End Sub End Module Class Point Private m_X As Integer Public Property X() As Integer Get X = m_X End Get Set(ByVal value As Integer) m_X = value End Set End Property Private m_Y As Integer Public Property Y() As Integer Get Y = m_Y End Get Set(ByVal value As Integer) m_Y = value End Set End Property End Class Class Line Private m_StartPoint As Point Public Property StartPoint() As Point Get StartPoint = m_StartPoint End Get Set(ByVal value As Point) m_StartPoint = value End Set End Property Private m_EndPoint As Point Public Property EndPoint() As Point Get EndPoint = m_EndPoint End Get Set(ByVal value As Point) m_EndPoint = value End Set End Property Public ReadOnly Property Length() As Double Get If StartPoint IsNot Nothing AndAlso EndPoint IsNot Nothing Then Dim side1 As Integer = Math.Abs(StartPoint.X - EndPoint.X) Dim side2 As Integer = Math.Abs(StartPoint.Y - EndPoint.Y) Length = Math.Sqrt(Math.Pow(side1, 2) + Math.Pow(side2, 2)) End If End Get End Property End Class 'Bezoek www.vbvoorbeelden.be voor meer Visual Basic voorbeelden. 'Copyright - De Wolf / vbvoorbeelden - 2003-2010 - Alle rechten voorbehouden.