What Are Properties?

One of the examples from the previous chapter was the class Horse, which had three fields: name, color, and height. We are showing only one constructor, in order to keep the example simple.

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

   Public Sub New(ByVal name As String, _
                  ByVal color As String, _
                  ByVal height As Single)
      m_name = name
      m_color = color
      If 1 <= height And height <= 2.5 Then
         m_height = height
      End If
   End Sub
End Class

In the constructor you are forcing the height to be a valid height for a horse (between 1 and 2.5 meters). That will ensure that your horse is a valid object. Well, actually, not quite so. The user can set the m_height field (which is a Single ...

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.