Classes in Visual Basic 2005

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.

how do you use a class ion a database application

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:
  1. Maintenance of code by introducing modularity.
  2. Encapsulation of internal complexities in code from end -users.
  3. Reuse of code across applications.
  4. 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.

  1. Select the Add Class option from the Project menu.
  2. Specify ItemClass.vb as the name of the class in the Add New Item-ItemDetailProject dialog box and click the Open button.
  3. To add members of a class in the code window of the class, we need to follow the below steps.
    1. Double-click the ItemClass.vb in the Solution Explorer window.
    2. In the code window of the ItemClass, type the following code.

VB 2005

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
End Class

C# 2005

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace FinCsharpProject
	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.

VB 2005
Public Class frmItemDetails
	Dim myItem As ItemClass = New ItemClass
Now we have instantiated the ItemClass object.

C# 2005

namespace FinCsharpProject
	public partial class frmItemDetails : Form
		public frmItemDetails()
				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
  1. Get procedures: Get procedures are used to retrieve the values from a property.
  2. 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.

C# 2005
  1. get method, which is used to retrieve the value from a member.
  2. get method, which is used to assign values to a member.


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
		Return ItemCode
	End Get
 'alled when property is set
	Set(ByVal value As String)
		ItemCode = value				
	End Set
End Property


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


get Accessor

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.
	return ItemCode;
set Accessor

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;

cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »