Visual Basic.NET 2008 > Building Custom Classes

Building Custom Classes in Visual Basic 2008

Jump to Table of Contents

Classes are practically synonymous with objects and they're at the very heart of programming with Visual Basic. The controls you use to build the visible interface of your application are objects, and the process of designing forms consists of setting the properties of these objects, mostly with point-and-click operations. The Framework itself is an enormous compendium of classes, and you can import any of them into your applications and use them as if their members were part of the language. You simply declare a variable of the specific class type, initialize it, and then use it in your code.

You have already worked with ListViewItem objects; they're the items that make up the contents of a ListView control. You declare an object of this type, then set its properties, and finally add it to the control's Items collection:

Dim LI As New ListViewItem
LI.Text = "Item 1"
LI.Font = New Font("Verdana", 12, FontStyle.Regular)

LI is an object of the ListViewItem type. The New keyword creates a new instance of the ListViewItem class; in other words, a new object. The following two statements set the basic properties of the LI variable. The Font property is also an object: it's an instance of the Font class. You can also add a few subitems to the LI variable and set their properties. When you're finished, you can add the LI variable to the ListView control:


Controls are also objects; they differ from other classes in that controls provide a visual interface, whereas variables don't. However, you manipulate all objects by setting their properties and calling their methods.

In this chapter, you'll learn how to do the following:

Build your own classes

Classes contain code that executes without interacting with the user. The class's code is made up of three distinct segments: the declaration of the private variables, the property procedures that set or read the values of the private variables, and the methods, which are implemented as Public subroutines or functions. Only the Public entities (properties and methods) are accessible by any code outside the class. Optionally, you can implement events that are fired from within the class's code. Classes are referenced through variables of the appropriate type, and applications call the members of the class through these variables. Every time a method is called, or a property is set or read, the corresponding code in the class
is executed.

Use custom classes in your projects

To use a custom class in your project, you must add to the project a reference to the class you want to use. If the class belongs to the same project, you don't have to do anything. If the class belongs to another project, you must right-click the project's name in the Solution Explorer and select Add Reference from the shortcut menu. In the Add Reference dialog box that appears, switch to the Browse tab and locate the DLL file with the class's implementation (it will be a DLL file in the project's Bin folder). Select the name of this file and click OK to add the reference and close the dialog box.

Customize the usual operators for your classes

Overloading is a common theme in coding classes (or plain procedures) with Visual Basic. In addition to overloading methods, you can overload operators. In other words, you can define the rules for adding or subtracting two custom objects, if this makes sense for your application.

Get Started >>

Table of Contents - Building Custom Classes in Visual Basic.NET 2008

  1. Classes and Objects
  2. What Is a Class?
  3. Building Your Own Class - The Minimal Class Example
  4. Adding Code to the Minimal Class
  5. Using Property Procedures when Building Custom Classes
  6. Customizing Default Members when Building Custom Classes
  7. Custom Enumerations
  8. Object Constructors
  9. Using a Project's Class in Other Projects
  10. Firing Events
  11. Instance and Shared Methods
  12. Discussion of a More Practical Class
  13. Converting Numbers to Strings
  14. Operator Overloading Copyright 2011 All Rights Reserved
  Home | Useful links | Contact us | Privacy Policy