A class is a conceptual representation of all the entities that share common attributes and behaviors. It defines the attributes and behaviors that are used by all the instances of that class. For example, we can create a class called Automobile that defines the common attributes, such as steering wheel, seating and number of wheels. A common behavior is transporting people.
The picture given below shows how a class template comprising of attributes and services is used as a template to create two classes named TranClass and ItemClass which form a part of the business layer.
To know how classes are used to organize business logic in a database application read : Database programming using visual basic, c# (c sharp) sql server
An object is an instance of a class. All the objects of a class have individual copies of the attributes and share a common set of behaviors. For example, Car and Jeep are objects of the class Automobile. Both these objects can have individual copies of steering wheel, seating and number of wheels.
Advantages of using Classes and Objects
There are a number of advantages of using classes and objects in software development. The most important advantages are:
- Maintenance of code by introducing modularity.
- Encapsulation of internal complexities in code from end -users.
- Reuse of code across applications.
- Support for a single interface to implement multiple methods.
Creating a Class in Visual Basic 2005 by adding a class to the project.
In Visual Basic 2005, we can add a class to a project by using the Project menu. After we select the Add Class option from the Project menu and specify a name for the class, the class gets declared automatically by the Visual Studio 2005 IDE.
- Select the Add Class option from the Project menu.
- Specify ItemClass.vb as the name of the class in the Add New Item-ItemDetailProject dialog box and click the Open button.
- To add members of a class in the code window of the class, we need to follow the below steps.
- Double-click the ItemClass.vb in the Solution Explorer window.
- In the code window of the ItemClass, type the following code.
Public Class ItemClass
Public ItemCode As String
Public ItemName As String
Public ItemDescription As String
Public ItemCategory As String
Public ItemPrice As Single
Public ItemUnit As String
public class ItemClass
public string ItemCode;
public string ItemName;
public string ItemDescription;
public string ItemCategory;
public double ItemPrice;
public string ItemUnit;
How to Instantiate a Class
After we have created a class and declared the member variables, we need to instantiate the class. To instantiate a class, we need to use the New keyword in VB 2005 and in C# 2005, we use the new keyword. The life of an object begins when an instance of a class is created using the New keyword. To create an object of the ItemClass, open the code window for the frmItemdetails form and type the following.
Public Class frmItemDetails
Dim myItem As ItemClass = New ItemClass
Now we have instantiated the ItemClass object.
public partial class frmItemDetails : Form
ItemClass myItem = new ItemClass();
Property Procedures in a class
A property procedure is a set of code statements that are used to assign or retrieve the values of the member variables declared within a Class. Properties are types of variables that store the values for an object of a Class.
We can access properties either as public variables or by using Set and Get property procedures. However, it is advantageous to use property procedures instead of public variables because, using a property procedure we can define a property as read-only, write-only, or read/write type. There are two types of property procedures in Visual Basic 2005 and C#.
Visual Basic 2005
- Get procedures: Get procedures are used to retrieve the values from a property.
- Set procedures: Set procedures are used to assign values to a property.
Using the Get and Set keywords, we can define property procedures in pairs. However, if we need the property to be read-only, we will define only the Get procedure. Similarly, if we need the property to be write-only, we will define only the Set procedure. Note that in C#, Get and Set keywords are case sensitive, i.e., get and set.
- get method, which is used to retrieve the value from a member.
- get method, which is used to assign values to a member.
PROPERTY DECLARATION IN VB 2005
A property is defined by a set of code enclosed within the Property and End Property statements. Within the Property and End Property statements, each property procedure appears as a block enclosed within the Get and End Get or the Set and End Set statements. First we need to change the access modifiers of the member variables to Private and write Get and Set property procedures as shown below.
Write the property procedures for the ItemClass as shown below.
Public Property Code() As String
'called when property is read
'alled when property is set
Set(ByVal value As String)
ItemCode = value
PROPERTY DECLARATION IN C# 2005
we will change the acess modifiers of all the member variables to private and write get and set accessors as shown below.
public class ItemClass
private string ItemCode;
private string ItemName;
private string ItemDescription;
private string ItemCategory;
private double ItemPrice;
private string ItemUnit;
To declare a property, write the property type and name followed by a pair of braces. Within the braces, we declare get and set accessors. Each accessor has an accessor body that retrieves and sets the property value. By default, properties have public access.
public string Code
The body of the get acccessor is similar to a class method that returns an object of the type of property. In the example, the accessor is similar to a method that returns a string. It returns the value of the private member variable in which the value of the property has been stored.
The set accessor sets the value of a property and is similar to a method that returns void. When we define a set accessor, we must use the value keyword to represent the arguement whose value is passed to and stored by the property.
ItemCode = value;