Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net
Basic Properties in VB.Net Basic Properties in VB.Net
Basic Properties in VB.Net
Go Back  Xtreme Visual Basic Talk > > > > Basic Properties in VB.Net


Reply
 
Thread Tools Display Modes
  #1  
Old 03-17-2004, 06:31 PM
excaliber's Avatar
excaliberBasic Properties in VB.Net excaliber is offline
Senior Contributor

* Expert *
 
Join Date: Nov 2002
Location: Ohio, USA
Posts: 1,828
Default Basic Properties in VB.Net


Introduction
So, what are Properties? And what are they good for? Properties are a way of encapsulating data structures in a class. Usually, we declare data structures (such as variables) inside of a class, and we want other classes to have access to that data. We could do this by creating helper functions to set and retrieve the data. Or we could directly access the data. Better yet, we could use what are called properties (which are essentially helper functions, except they have loads of other useful features). It's better programming practice to indirectly access the data in classes. Properties allow you to indirectly access the data of a class, and allow additional coding before the value is set/returned.

A Basic Property
Lets take a look at a basic property in a class.

Code:
 Public Class myClass
 
 Private strName as string
 
    Public Property Name()
 	   Get
 		 Name() = strName
 	   End Get
 
 	   Set(ByVal Value)
 		 strName = Value
 	   End Set
    End Property
 
 End Class
Lets look at this code. We have a class named myClass. And we have a property called Name. A property has two parts (unless its ReadOnly/WriteOnly, which we will get to later), a Get and a Set portion. As we mentioned earlier, properties typically encapsulate data, and in this case it is providing a way to access the data in strName (notice how the variable is declared as private to the class. We want only the class to have direct access to the data)

Under the Get portion, we see that we set the property (Name) equal to the local strName. This returns the value of strName to the caller. Under the Set portion, we set the local strName variable equal to Value. Value is a default variable that is created for properties, and holds the information passed by the caller.

Simple enough. Lets see how to use the property in code.

Code:
 'declare and create a new instance of the class
 Dim clsTest as myClass
 clsTest = new myClass()
 
 'Set the name property in the class to "Bob"
 'This utilizes the Set portion
 clsTest.Name() = "Bob"
 
 'Retrieve the data from the Name property
 'This utilizes the Get portion
 MsgBox(clsTest.Name)
Properties are not limited to merely setting and retrieving data. They can contain code that can do a variety of tasks (for instance, if you want different data returned based on other things). You can include code and even catch exceptions in the property.

ReadOnly Properties
But what if you want a property to be read only (no Set portion)? Heres how:

Code:
 Public ReadOnly Property Name()
 	Get
 		'Your code here
 	End Get
 End Property
Simple as that. Just add the ReadOnly keyword before the Property keyword, and remove the Set portion of code.


WriteOnly Property
The WriteOnly property is near identical to the ReadOnly:

Code:
 Public WriteOnly Property Name()
 	Set
 		'Your code here
 	End Set
 End Property

Static Properties
Regular properties and data structures belong to the objects of the class (each new instance of the class has a new set of properties and data structures, independent of other classes). But sometimes you have a set of class instances that require shared data. Essentially, this means that each instance of the class has access to the data declared as Shared (instead multiple sets of data structures, there is one accessible by all class instances). Heres how to set up a property to access that shared variable:

Code:
 Public Class myClass
 
 'note the Shared keyword in the declaration of the variable and the property
 
 Private Shared strName as string
 	Public Shared Property Name()
 			Get
 			Name() = strName
 			End Get
 
 			Set(ByVal Value)
 			strName = Value
 			End Set
 		End Property
 End Class
Thats it. Everything else functions as a normal property, except its access the shared data from the class.

This just covers the basic usage of properties. There are many other features, such as Inheritance, Polymorphism, and Abstract Properties. But this will get you going.
__________________
RandomIRC - Your neighborhood's friendly IRC channel (irc.randomirc.com - #code)

"Perl - The only language that looks the same before and after RSA encryption."
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic VB.NET Syntax Question KintyHill .NET General 4 03-11-2004 06:02 PM
Passing variables and controls using WinForms and VB.NET Bucky Tutors' Corner 0 02-24-2004 04:45 PM
Advanced properties for PrintOut() Oracle Word, PowerPoint, Outlook, and Other Office Products 2 07-16-2003 06:25 AM
how to access mesh properties in VB.NET d1820 DirectX 1 04-25-2003 02:39 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net Basic Properties in VB.Net
Basic Properties in VB.Net
Basic Properties in VB.Net
 
Basic Properties in VB.Net
Basic Properties in VB.Net
 
-->