Define and use Class: Property
Imports System Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.IO Imports System.Drawing.Printing Public Class MainClass Shared Sub Main() Dim objCar As New Car 'Set the Color property to Red objCar.Color = "Red" 'Show what the value of the property is Console.WriteLine("My car is this color:") Console.WriteLine(objCar.Color) 'Report the speed Console.WriteLine("The car's speed is:") Console.WriteLine(objCar.Speed) 'Accelerate objCar.Accelerate(5) 'Report the new speed Console.WriteLine("The car's speed is now:") Console.WriteLine(objCar.Speed) 'Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors) 'Try changing the number of doors to 1000 objCar.NumberOfDoors = 1000 'Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors) 'Now try changing the number of doors to 2 objCar.NumberOfDoors = 2 'Report the number of doors Console.WriteLine("The number of doors is:") Console.WriteLine(objCar.NumberOfDoors) 'Accelerate the car to 25mph objCar.Accelerate(25) 'Report whether or not the car is moving If objCar.IsMoving = True Then Console.WriteLine("The car is moving.") Else Console.WriteLine("The car is stopped.") End If End Sub End Class Public Class Car Implements IDisposable Public Color As String Public HorsePower As Integer Private _speed As Integer Private _numberOfDoors As Integer Public ReadOnly Property Speed() As Integer Get Return _speed End Get End Property Public Sub Accelerate(ByVal accelerateBy As Integer) 'Adjust the speed _speed += accelerateBy End Sub Public Property NumberOfDoors() As Integer Get Return _numberOfDoors End Get Set(ByVal value As Integer) 'Is the new value between two and five If value >= 2 And value <= 5 Then _numberOfDoors = value End If End Set End Property Public Function IsMoving() As Boolean 'Is the car's speed zero? If Speed = 0 Then Return False Else Return True End If End Function 'Constructor Public Sub New() 'Set the default values Color = "White" _speed = 0 _numberOfDoors = 5 End Sub Public Overridable Function CalculateAccelerationRate() As Double Return 4.2 End Function Private disposed As Boolean = False ' IDisposable Private Overloads Sub Dispose(ByVal disposing As Boolean) If Not Me.disposed Then If disposing Then ' TODO: put code to dispose managed resources End If ' TODO: put code to free unmanaged resources here End If Me.disposed = True End Sub #Region " IDisposable Support " ' This code added by Visual Basic to correctly implement the disposable pattern. Public Overloads Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub Protected Overrides Sub Finalize() ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(False) MyBase.Finalize() End Sub #End Region End Class
1. | Declare Protected Properties | ||
2. | Shared Property Demo | ||
3. | Get and set Properties | ||
4. | Class Property Get and Set | ||
5. | Property Shadow during Inheritance | ||
6. | Compare int property |