How is a class declared in Visual Basic

How is a class declared in Visual Basic

Public Class Foo

End Class
Object-Oriented Programming Overview
Object-Oriented Programming (OOP) is a method of software design and construction. It is the next logical progression, after structured programming, to improve your code reusability and maintainability. Put another way, OOP is a method of designing individual software components (classes) with associated behaviors (methods) and data limitations (properties), and that helps you piece these components together to create a complete application.

The best thing about OOP is that it allows you to group data into discrete variables contained within a class in your program. This data is separate and distinct from any other class in your application. No class can interfere with the data in any other class without going through a specific interface on that class.

Each object defines its functionality as a set of properties and methods that it will respond to. Other code can call these methods on the object and have them perform some behavior and use the properties to retrieve or change some information. In this way, code cannot affect the information or processes of other objects directly. As you build a class, you will see how to build this functionality using properties and methods.

Table 1 is a review of OOP terms that you should already be familiar with prior to reading this document.

Table 1: OOP Terms

Class A container for data and code. The data within the class can be accessed with properties. The code is referred to as methods.
Object An instance of a class in memory. An instance is created using a Dim statement and the New keyword.
Constructor A procedure that is automatically invoked when an object is first instantiated. In Visual Basic 6.0, the constructor was called Class_Initialize. In Visual Basic .NET the constructor is called New.
Destructor A procedure that is automatically invoked when an object is destroyed. In Visual Basic 6.0, the destructor was called Class_Terminate. In Visual Basic .NET, the destructor is called Finalize.
Properties A routine exposed by an object to expose data, and to allow code outside the object to affect the objects data.
Method An action that can be performed by an object. In Visual Basic .NET, methods are defined as Subs and Functions.

Good Uses for Classes
Classes are the heart and soul of an object-oriented language. You will find yourself using classes whenever you write even the simplest of programs in Visual Basic .NET. The Microsoft .NET Framework makes extensive use of classes, and so should you. Below are some common uses of classes:

•Wrapping up the representation and set of operations you perform on a database table, for example adding, editing, deleting, and retrieving data.
•Wrapping up the set of operations and data for dealing with text files such as reading, writing, and indexing the lines of text within the file.
•Wrapping up all global variables in a program into properties within a class. This can help with keeping track of the amount of "free-floating" globals that somehow seem to work their way into many programs.
Create a Class
Let's create a class representing a line of text. To do this, you create a property to return the line of text and a read-only property that returns the length of the text. You will also create a method that returns the first word in the line. As you perform all of these steps, you will learn the correct way to create a class. You will build a form like the one shown in Figure 1 to test the Line class as you build it.

  • Back aarticle:
  • Next aarticle: No
  • Copyright © 2007-2012 www.chuibin.com Chuibin Copyright