Properties in VB.net
By: Steven Holzner in VB.net Tutorials on 2010-11-17
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
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments