|
|
|
|
|
|
Author Rank:
|
|
Total page views :
241649
|
|
Total downloads :
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
In VB.NET, properties are nothing but natural extension of data fields. They are usually known as 'smart fields' in VB.NET community. We know that data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language. In VB.NET, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members.
Usually inside a class, we declare a data field as private and will provide a set of public SET and GET methods to access the data fields. This is a good programming practice, since the data fields are not directly accessible out side the class. We must use the set/get methods to access the data fields.
An example, which uses a set of set/get methods, is shown below.
'SET/GET methods Imports System Class [MyClass] Private x As Integer Public Sub SetX(ByVal i As Integer) x = i End Sub 'SetX Public Function GetX() As Integer Return x End Function 'GetX End Class '[MyClass] Class MyClient Public Shared Sub Main() Dim mc As New [MyClass] mc.SetX(10) Dim xVal As Integer = mc.GetX() Console.WriteLine(xVal) 'Displays 10 End Sub 'Main End Class 'MyClient
But VB.NET provides a built in mechanism called properties to do the above. In VB.NET, properties are defined using the property declaration syntax. The general form of declaring a property is as follows.
<acces_modifier> <return_type> <property_name> Get End Get
Set End Set
Where <access_modifier> can be private, public, protected or internal. The <return_type> can be any valid VB.NET type. Note that the first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor.
For example the above program can be modifies with a property X as follows.
Class MyClass Private x As Integer Public Property X() As Integer Get Return x End Get Set(ByVal Value As Integer) x = value End Set End Property End Class 'MyClass
The object of the class MyClass can access the property X as follows.
Dim mc As New [MyClass] mc.X = 10 ' calls set accessor of the property X, and pass 10 as value of the standard field "value".This is used for setting value for the data member x. Console.WriteLine(mc.X) ' displays 10. Calls the get accessor of the property X.
The complete program is shown below.
'VB.NET: Property Imports System Class MyClass Private x As Integer Public Property X() As Integer Get Return x End Get Set(ByVal Value As Integer) x = value End Set End Property End Class '[MyClass] Class MyClient Public Shared Sub Main() Dim mc As New MyClass() mc.X = 10 Dim xVal As Integer = mc.X Console.WriteLine(xVal) 'Displays 10 End Sub 'Main End Class 'MyClient
Remember that a property should have at least one accessor, either set or get. The set accessor has a free variable available in it called value, which gets created automatically by the compiler. We can't declare any variable with the name value inside the set accessor.
We can do very complicated calculations inside the set or get accessor. Even they can throw exceptions.
Since normal data fields and properties are stored in the same memory space, in VB.NET, it is not possible to declare a field and property with the same name.
Static Properties
VB.NET also supports static properties, which belongs to the class rather than to the objects of the class. All the rules applicable to a static member are applicable to static properties also.
The following program shows a class with a static property.
'VB.NET : static Property Imports System Class MyClass Private Shared x As Integer Public Shared Property X() As Integer Get Return x End Get Set(ByVal Value As Integer) x = value End Set End Property End Class 'MyClass Class MyClient Public Shared Sub Main() MyClass.X = 10 Dim xVal As Integer =MyClass .X Console.WriteLine(xVal) 'Displays 10 End Sub 'Main End Class 'MyClient
Remember that set/get accessor of static property can access only other static members of the class. Also static properties are invoking by using the class name.
Properties & Inheritance
The properties of a Base class can be inherited to a Derived class.
'VB.NET : Property : Inheritance Imports System Class Base Public Property X() As Integer Get Console.Write("Base GET") Return 10 End Get Set(ByVal Value As Integer) Console.Write("Base SET") End Set End Property End Class 'Base Class Derived Inherits Base End Class 'Derived Class MyClient Public Shared Sub Main() Dim d1 As New Derived d1.X = 10 Console.WriteLine(d1.X) 'Displays 'Base SET Base GET 10' End Sub 'Main End Class 'MyClient
The above program is very straightforward. The inheritance of properties is just like inheritance any other member.
Properties & Polymorphism
A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.
'VB.NET : Property : Polymorphism Imports System Class Base Public Overridable Property X() As Integer Get Console.Write("Base GET") Return 10 End Get Set(ByVal Value As Integer) Console.Write("Base SET") End Set End Property End Class 'Base Class Derived Inherits Base Public Overrides Property X() As Integer Get Console.Write("Derived GET") Return 10 End Get Set(ByVal Value As Integer) Console.Write("Derived SET") End Set End Property End Class 'Derived Class MyClient Public Shared Sub Main() Dim b1 = New Derived b1.X = 10 Console.WriteLine(b1.X) 'Displays 'Derived SET Derived GET 10' End Sub 'Main End Class 'MyClient
Abstract Properties
A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
The following program shows an abstract property in action.
'VB.NET : Property : Abstract Imports System MustInherit Class Abstract Public MustOverride Property X() As Integer Get End Get Set End Set End Property End Class 'Abstract Class Concrete Inherits Abstract Public Overrides Property X() As Integer Get Console.Write(" GET") Return 10 End Get Set(ByVal Value As Integer) Console.Write(" SET") End Set End Property End Class 'Concrete Class MyClient Public Shared Sub Main() Dim c1 As New Concrete c1.X = 10 Console.WriteLine(c1.X) 'Displays 'SET GET 10' End Sub 'Main End Class 'MyClient
The properties are an important features added in language level inside VB.NET. They are very useful in GUI programming. Remember that the compiler actually generates the appropriate getter and setter methods when it parses the VB.NET property syntax.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
Rajesh VS
Rajesh V.S is a software engineer in the area of C/C++/JAVA for the last 5 years. Currently he is interested in core C# language. He is Sun Certified Java Programmer. His other area of interest includes Design Patterns and CORBA. He is also a writer of numerous articles for many technical Web sites.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from
DevExpress - Absolutely Free-of-Charge without any royalties or distribution
costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin
edit, tab control and so much more!
DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
|
|
|
|
|
|
|
|
|
|
|
|
|