The Property Statement

A solution to the problem is given in the following example.

Public Class Horse
   Public m_name As String
   Public m_color As String
   Private m_height As Single = 1.5

   Public Property height() As Single
						Get
						Return m_height
						End Get
						Set
						If 1 <= Value And Value <= 2.5 Then
						m_height = Value
						End If
						End Set
						End Property

   Public Sub New(ByVal name As String, _
                  ByVal color As String, _
                  ByVal height As Single)
      m_name = name
      m_color = color
      MyClass.height = height
   End Sub
End Class

The first thing to observe is that the m_height field is now private; it can be accessed from the members of the class, but it will be invisible to anyone outside the class.

Then you have added the Property statement, which is somewhat similar to that of a ...

Get Visual Basic® .NET by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.