ARTICLE

Understanding and using Properties in VB.NET

Posted by Rahul Sharma Articles | Visual Basic .NET November 10, 2012
In this article, you will learn about properties in VB.NET. Here you will learn how to define and use properties.
Reader Level:

Well! I'll explain the concept of properties with some examples to make a clear understanding.

So First of all: What are properties?

Just normal variables, like other primitive variables!

Example A:

''''''''''''' ExA.vb ''''''''''''''''''''
Imports System
Namespace Rahul
Class Colors
Public Shared Sub Main()
Dim UrColorAs New UrChoice
'Overwriting your favourite color to yellow
UrColor.Color = "Yellow"
Console.WriteLine(("Your favourite color is " + UrColor.Color))
End Sub 'Main
End Class 'Colors
Public Class UrChoice
' Want to provide the access to everybody.
Public Color As String
' Default Constructor setting up your favourite color
Public Sub New()
Color = "White"
End Sub 'New
End Class 'UrChoice
End Namespace 'Rahul

Compile the program as: vbc ExA.vb.

Now type ExA and press enter (return) key...

Output is: Your favorite color is Yellow.

In Example 'A', I have declared two classes in Rahul namspace:

One is UrChoice (as the names are self explanatory) is talking about your choice, not all of your choices but here we are discussing about your favorite color and you have fixed your favorite color as "White" (Just for an instance). Now everybody is free to know about your favorite color and he can tell others about your choice.

But there is a problem!

Somebody has changed the value of your favorite color and now your favorite color is Yellow.

Hey! That's cheating!!!!

Yes! Of course but is there any way to protect your variable or your choice?

Here comes the turn of properties.

Properties are like normal variables but with more power and flexibility.

Now we convert the color variable in a property with the same name.

Example B:

'''''''''''''' ExB.vb '''''''''''''
Imports System
Namespace Rahul
Class Colors
Public Shared Sub Main()
Dim UrColorAs New UrChoice
'Overwriting your favourite color to yellow
UrColor.Color = "Yellow"
Console.WriteLine(("Your favourite color is " + UrColor.Color))
End Sub 'Main
End Class 'Colors
Public Class UrChoice
' your private variable to store your favourite color
Private MyColor As String
' Defining property, Want to provide the access to everybody.
Public ReadOnly Property Color()As String
Get
Return
MyColor
End Get
End
Property
' Default Constructor setting up your favourite color
Public Sub New()
MyColor = "White"
End Sub 'New
End Class 'UrChoice
End Namespace 'Rahul

This time we didn't define color variable as a normal variable rather we defined a property named Color.

But in this case we had to define a new variable MyColor to store the initial and right value of your favorite color (also making it private to hide it from outside world).

This time when we try to compile our program as: vbc ExB.vb.

We get an error:

Property or indexer 'Rahul.UrChoice.Color' can not be assigned to -- it is read only.

So, we got actually what we wanted. Now if somebody tries to modify your favorite color, he can not do this.

If we discuss the syntax of the property, this is really very easy.

Decide a name to the property that can reflect its use and the Data Type of the property and start with the braces, Like in our example:

' Declaring its scope as public so that everybody can use it.
' Data Type is string

Public WriteOnly ReadOnly Property Color() As String
End Property

Property or indexer declaration must contain at least one accessorNow put the value of the variable in the return statement of the get block, so that somebody can only get the value but can not write the value to the property. In our example:

Public ReadOnly Property Color()As String
Get
Return
MyColor
End Get
End
Property

That's it!!!

Can we do the same security of our data by some other means....

Yes! I'll be discussing another method to do the same thing in the PART 2 of this series and also about the "set" block of a property, so that we can make a variable in which everybody can write but can not read.

Hey! This is not all about properties, there is a lot...

Something more than this!

Will continue in PART 2....

 

COMMENT USING