O'Reilly logo

Visual Basic® .NET by Example by Bob Donald, Gabriel Oancea

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required