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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.