(Note: This blog post was originally published under my old domain(codesmiles.com), here. Web Archive link.
(This post is part of my Visual Studio 2010 series)
In C# we can create properties for classes in simple way by just specifying the below code, this option is called auto-implemented property, as the implementation is taken care by the compiler.[more]
public int Price { get; set; }
In Visual Basic we don’t have such simple option to create properties, hence to create a simple class we have to write the below code..
Class Client
Private _Code As String
Public Property Code() As String
Get
Return _Code
End Get
Set(ByVal value As String)
_Code = value
End Set
End Property
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _CreditLimit As Single = 2000
Public Property CreditLimit() As Single
Get
Return _CreditLimit
End Get
Set(ByVal value As Single)
_CreditLimit = value
End Set
End Property
End Class
But in Visual Basic 2010(in Visual Studio 2010) we can write the below code instead of the above..
Class Client
Public Property Code As String
Public Property Name As String
Public Property CreditLimit As Single = 2000
End Class
See how much simplicity and clarity auto-implemented properties can give to your classes when compared to your manually-implemented properties.
Most of the business entity classes I have seen in projects have such simple properties, I mean, only few require some custom logic to be included in the property method, like the one below..
Private _CreditLimit As Single = 2000
Public Property CreditLimit() As Single
Get
Return _CreditLimit
End Get
Set(ByVal value As Single)
If value > 5000 Then
Throw New Exception("CreditLimit cannot be greater than 5000")
End If
_CreditLimit = value
End Set
End Property
So if you are a Visual Basic coder you can start creating your classes, particularly your business entities with such simple code in Visual Basic 2010.