excaliber
03-17-2004, 05:31 PM
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.
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.
'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:
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:
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:
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.
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.
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.
'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:
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:
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:
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.