Properties in VB.net

By: Steven Holzner Emailed: 1757 times Printed: 2448 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Visual Basic objects can have methods, fields, and properties. If you've worked with Visual Basic before, you're familiar with properties, which you use to set configuration data for objects, such as the text in a text box or the width of a list box. Using properties provides you with an interface to set or get the value of data internal to an object. You declare properties using Get and Set procedures in a Property statement (and, as you might expect, the syntax has changed from VB6):

[ <attrlist> ] [ Default ] [ Public | Private | Protected | Friend |
Protected Friend ] [ ReadOnly | WriteOnly ] [Overloads | Overrides ]
[Overridable | NotOverridable] | MustOverride | Shadows | Shared] Property
varname([ parameter list ]) [ As typename ] [ Implements interfacemember ]
   [ <attrlist> ] Get
       [ block ]
   End Get
   [ <attrlist> ] Set(ByVal Value As typename )
       [ block ]
   End Set
End Property

Here are the parts of this statement that are different from the keywords used in the Sub statement. (See the Immediate Solution "Creating Sub Procedures"):

  • Default—Makes this a default property. Default properties can be set and retrieved without specifying the property name, and must accept parameters.

  • ReadOnly—Specifies that a properties value can be retrieved, but it cannot be the modified. ReadOnly properties contain Get blocks but no Set blocks.

  • WriteOnly—Specifies that a property can be set but its value cannot be retrieved. WriteOnly properties contain Set blocks but no Get blocks.

  • varname—A name that identifies the Property.

  • parameter list—The parameters you use with the property. The list default is ByVal.

  • typename—The type of the property. If you don't specify a data type, the default type is Object.

  • interfacemember—When a property is part of a class that implements an interface, this is the name of the property being implemented.

  • Get—Starts a Get property procedure used to return the value of a property. Get blocks are optional unless the property is ReadOnly.

  • End Get—Ends a Get property procedure.

  • Set—Starts a Set property procedure used to set the value of a property. Set blocks are optional unless the property is WriteOnly. Note that the new value of the property is passed to the Set property procedure in a parameter named Value when the value of the property changes.

  • End Set—Ends a Set property procedure.

Visual Basic passes a parameter named Value to the Set block during property assignments, and the Value parameter contains the value that was assigned to the property when the Set block was called. Here's an example where I'm creating a read/write property named Prop1 in Module2, and storing the property's value in a private text string named PropertyValue in Module2:

Module Module1
    Sub Main()
        
    End Sub
End Module

Module Module2
    Private PropertyValue As String
    Public Property Prop1() As String
        Get
            Return PropertyValue
        End Get
        Set(ByVal Value As String)
            PropertyValue = Value
        End Set
    End Property
End Module
Tip 

When you type the first line of a property procedure, as Public Property Prop1() As String here, VB .NET will add a skeleton for the Get and Set procedures automatically.

Now I can refer to Prop1 of Module2, setting it and reading its value, like this:

Module Module1
    Sub Main()
        Module2.Prop1 = 2
        System.Console.WriteLine("Prop1 = " & Module2.Prop1)
        System.Console.WriteLine("Press Enter to continue...")
    End Sub
End Module

Module Module2
    Private PropertyValue As String
    Public Property Prop1() As String
        Get
            Return PropertyValue
        End Get
        Set(ByVal Value As String)
            PropertyValue = Value
        End Set
    End Property
End Module

This console application displays this text in a DOS window:

Prop1 = 2
Press Enter to continue...

You also can index properties by passing an index value when referring to a property. Here's an example; in this case, I'm creating a property array by adding an index value that you must specify each time you use the property:

Public Module Module1
    Private Data(200) As Integer

    Public Property Property1(ByVal Index As Integer) As Integer
        Get
            Return Data(Index)
        End Get
        Set(ByVal Value As Integer)
            Data(Index) = Value
        End Set
    End Property
End Module

Now instead of referring to the property simply as Property1, I must use an index value, such as Property1(5), which refers to a particular element in the property array: Scope, and Exception

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Module1.Property1(5) = 1
        MsgBox(Module1.Property1(5))
    End Sub

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

as in property we set the value and then retrieve it using 'get'. then why we should use get before set ?? during programing it is tedious to write code for get before writing code for set.. can we use set and then use get? reply..

View Tutorial          By: Deep at 2011-03-18 21:00:54
2. View Comment

dear! can you show me about "How to start as programmer in vb.net?"because i want to be a programmer in the future.now i'm 22 years old and live in the Cambodia.

View Tutorial          By: vatha chhean at 2011-05-31 07:34:42

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
How to export from DataGridView to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
How to export from database to excel using VB.net
Insert cell data in an Excel file using OLEDB in VB.net
The Option and Imports Statements in VB .NET
Handling Dates and Times in VB.net
Sub Procedures and Functions in VB.net
A tutorial on Chat Server and Chat Client in VB.net
To read the entire worksheet in an Excel workbook through VB.net Code
What's New in VB .NET? A comparison of VB vs VB.net
Arrays and Dynamic Arrays in VB.net
How To Connect To A MySql Database in VB.net
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Using Resume Next and Resume Line in VB.net
For Loop in VB.net
Most Emailed Articles (in VB.net)
How to export from DataGridView to excel using VB.net
How to send email using VB.NET code
Data types in VB.net
Arrays and Dynamic Arrays in VB.net
If…Else Statements in VB.net
ArrayList data structure in VB.net
Name Value Collecion in VB.net
Implicit & explicit type Conversion in VB.net
String equals, Copy to & Copy to in VB.net
File stream operations in VB.net
Vb.net binary reader and writer
Insert cell data in an Excel file using OLEDB in VB.net
Update cell data in an Excel file using OLEDB in VB.net
How to find IP Address of Host using .net code
Specifying Optional Arguments with default values in Procedures in VB.net