Array of objects: table Learn programming Visual Basic (VB.net)

Lesson:

OOP More On Classes


Exercise:

Array of objects: table


Objetive:

Create a class named "Table". It must have a constructor, indicating the width and height of the board. It will have a method "ShowData" which will write on the screen the width and that height of the table. Create an array containing 10 tables, with random sizes between 50 and 200 cm, and display all the data.


Code:

Imports System
Namespace ArrayOfObjects
    Class Table
        Private width, height As Single

        Public Sub New()
        End Sub

        Public Sub New(ByVal width As Single, ByVal height As Single)
            Me.width = width
            Me.height = height
        End Sub

        Public Property Width As Single
            Set(ByVal value As Single)
                width = value
            End Set
            Get
                Return width
            End Get
        End Property

        Public Property Height As Single
            Set(ByVal value As Single)
                height = value
            End Set
            Get
                Return height
            End Get
        End Property

        Public Sub ShowData()
            Console.WriteLine("Width: {0}, Heigth: {1}", width, height)
        End Sub
    End Class

    Class TestTables
        Private Shared Sub Main()
            Dim debug As Boolean = False
            Dim myTables As Table() = New Table(9) {}
            Dim rnd As Random = New Random()

            For i As Integer = 0 To 10 - 1
                myTables(i) = New Table(rnd.[Next](50, 201), rnd.[Next](50, 201))
                myTables(i).ShowData()
            Next

            If debug Then Console.ReadLine()
        End Sub
    End Class
End Namespace

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.