Properties in VB.net

By: Steven Holzner Viewed: 153420 times  Printer Friendly Format    


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


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial