1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - Objects in Detail

The Class is the VB.NET Object

By Dan Mabbutt, About.com

Feb 19 2008

Now is the time to also get more detailed about how to create an object in VB.NET. We've already seen that a VB.NET Class is the way to code an object in VB.NET. And, since "everything is a object" there's no way to avoid using a Class in even the simplest VB.NET code. That's why we've seen them right from the beginning in this tutorial. Since I haven't explained the code for the Class, here's that explanation.

The basic Class in VB.NET is very simple:

Public name Class
<... the class code goes here ...>
End Class

There are a lot of options to confuse you if you read the syntax definition at MSDN:

[<attributelist>][accessmodifier][Shadows][MustInherit|NotInheritable][Partial] _
Class name [(Of typelist)]
   [Inherits classname]
   [Implements interfacenames]
   [statements]
End Class

You probably won't use most of these options most of the time. Like most things in programming, the basic idea is elegantly simple. The details are complex and confusing. Stick to the basic idea at first.

The first basic idea was first introduced in the simple Hello World program in part 5: Properties and Methods. I'm going to cover those important points in greater depth here.

Coding a Property

Since a Property must be controlled by the class, there are two essential elements for it.

  1. A way for the Class to hold the information.
  2. A way to Get and Set the Property information from outside the Class.

VB.NET has a special syntax that is used just for Properties. Since the code is very standardized, VB.NET Express will enter it for you. Just enter the code (with a property name and data type to fit your application) ...

Property <property name> As <data type>

When you press "Enter," VB.NET will fill in the rest.

Public Class Class1
   Public Property aProperty() As String
      Get

      End Get
      Set(ByVal value As String)

      End Set
   End Property
End Class

The Get and Set Property Procedures allow VB code that is outside the Class to change and retrieve the value of the Property ... but you have to add the code in the procedures that make it happen. That code goes inside the Get and Set blocks that VB.NET fills in.

The actual value of the Property is held in a Private member variable declared inside the Class:

Private m_ClassProperty as data type

If a Property is just a simple value, then the code in the Property Procedure is very simple too. You can see that in the previous example in part 5

Sharp-eyed students might notice that it would be a lot easier to simply code ...

Public m_ClassProperty as datatype

... and avoid the Property Procedure code entirely. But that would violate the OOP principals! Some example reasons we do it this way include:

  • Unauthorized access to the information can be controlled.

Suppose the property is a login ID. You might want to be sure that any ID is authenticated very carefully. You could code a complete login authentication into the Get procedure. But if the ID could just be changed from outside the object, the code would not guarantee that all ID's were authenticated.

  • Error checking can be used.

Before the value is changed, the Set procedure could check anything about the new value. For example, the procedure could check to see if an account name was already in the database.

And there are zillions of other reasons like these. If Get and Set Property Procedures aren't used, these things couldn't be done.

Coding a Method

A method is just a Sub or a Function inside the Class that does something. Since it's intended to be used by code outside the Class, it's normally Public. And since it's inside the Class, the method has access to anything else that is also in the Class, such as the Private variables that hold the current values of properties.

And it can be updated by simply updating the Class. No other code has to be involved.

For example, suppose a Class contained a method called CreateAccount(NewAccountName) that creates a unique key to the database using NewAccountName as a seed. Now suppose that the method has a bug in it and occasionally creates a duplicate key. After the bug is fixed (with bug-free code in the method), only this one Class has to be updated. No other code has to be touched.

A complete object will normally include at least one special method that always has the name New. In part 5, I wrote, "A programmer can make as many copies (instances) of an object as are needed - this is called instantiation." Normally, an instance, that is a copy, of the object is used in other code. The Class code is just there as a mold to make more instances when they're needed. VB.NET writes the code to call the New method when another instance of the Class is needed.

One of the main benefits is the ability to do any special initialization for the object. Instantiating (making a copy of an object) using New is usually done like this:

Dim <instance name> as New <object name>

There is an example of New in the Signature Block program explained on the next page.

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - Objects in Detail

©2008 About.com, a part of The New York Times Company.

All rights reserved.