Home and Learn: VB Net Course


Create your own Classes in VB .NET

If you haven't yet read the introduction to Classes, here it is: VB .NET Classes and Objects.

The big benefit of an Object Oriented Programming language is that you can create your own Objects. (It's an Object when you're using the code, remember, and a Class when you're not.)

We'll see how to do that now, as we create a very simple Class.

Start a new Windows Forms project for this. On your new form, add a button. We'll use the button to test our classes and objects.

Adding a new class to your project is easy. There are a couple of ways to do so. You can either click Project from the menus at the top of Visual Studio, then select Add Class. Or you can right-click your project name in the Solution Explorer on the right and choose Add > Class from the menu that appears. Whichever way you choose, you should see the following dialog box appear:

The New Item dialog Box in Visual Basic .NET showing how to add a class.

Click on Common Items from the choices on the left. Then select Class from the items that appear in the middle. In the Name box at the bottom, type SimpleStaticClass as the name. Click the Add button when you're done.

Visual Studio adds a new class file to your VB Net project. There's not much to see, just two lines of code:

Public Class SimpleStaticClass

End Class

The code your write for your class obviously goes between the two lines.

The class we'll create first is, as the name states, a simple static class. These are classes that are very much frowned upon, as they're not really a class that you turn into an object. They are just classes that can hold all your functions and subs. If all you want is a place to store your long and messy functions and subs to keep them out of the way then static classes are fine. But be aware that they are the poor, Hillbilly cousin of the Object Oriented world.

But let's see how they work, nevertheless.

To your new class, add the following Subroutine:

Public Class SimpleStaticClass

Public Sub DisplayMessage()

MessageBox.Show("Welcome to the Class")

End Sub

End Class

It's just a Public Sub with a message box as the code. (If you made it Private, then the Sub would not be able to be seen from outside of this class.)

If you squint really hard at the code in Visual Studio, you should be able to make out some little dots beneath the first few letters of DisplayMessage:

Instance Data indicator in VB NET

Hold your mouse over the first few letters and you should see this daunting warning appear:

Popup VB Net message suggesting a Subroutine can be marked as static.

We'll get to what Instance Data means soon. But, to mark this Sub as static, keep your mouse over the first few letters and you should see an icon of a lightbulb appear:

How to mark a Subroutine as static.

Click the down arrow to see a list of fixes:

Visual Studio helper to convert a Subroutine to a staic one.

The highlighted box shows you what Visual Studio thinks the fix should be - adding the word Shared before the word Sub. Click on Make static to implement the fix. Your Sub should then turn into this and the dots under the first few letters should disappear:

Public Shared Sub DisplayMessage()

MessageBox.Show("Welcome to the Class")

End Sub

Now go back to your form with the button on it. Double-click your button to create a code stub.

Because this class contains just a static subroutine, you don't need to create an object from this class to use the sub. All you need to do is first type the name of the class. Type SimpleStaticClass, then type a dot. As soon as you type the dot, you should see an Intellisense list appear:

Intellisense list in Visual Studio with custom Sub on it.

You should see the name of your Sub on the list. It's there because you made it public. If you had made the Sub private, then it wouldn't appear as a list item.

Double-click DisplayMessage, and add a pair of empty round brackets on the end. Your single line of code for your button should look like this:

SimpleStaticClass.DisplayMessage()

SimpleStaticClass will turn a greeny color in your code window. It's turned green because Visual Studio knows that this is a class, and classes get the green treatment in VS.

 

Overloading a Subroutine in Visual Basic .NET

Now add this Sub to your class (not the button):

Public Shared Sub DisplayMessage(name As String)

MessageBox.Show("Welcome to the Class, " + name)

End Sub

Notice that it has exactly the same name as the first Sub, DisplayMessage. The difference is that we've now added a parameter between the round brackets of the Sub - name As String. When you set up Subs or Functions with the same name, it's called Overloading. You can have as many Subs or Functions with the same name as you want. But the rule is that that they can't have identical parameters. We can't have this:

Public Shared Sub DisplayMessage(title As String)

MessageBox.Show("Welcome to the Class, " + title)

End Sub

The parameter this time is called title, But, because it's also set up as a string, and it's the only parameter, Visual Studio sees it as having the same signature. So it's not allowed. You can have this, though:

Public Shared Sub DisplayMessage(title As Integer)

MessageBox.Show("Welcome to the Class, " + title.ToString())

End Sub

This time, although the Sub has the same name, DisplayMessage, the parameter is an Integer. Because it's an Integer, Visual Studio sees it has having a different signature, so it's OK.

You can have as many parameters as you want, just like normal Subs and Functions. But, if they are in the same class, and have the same name, just make sure the parameters are different.

One good example of an overloaded Function is the Show function of the MessageBox you have been using. If you highlight the word Show, you should see this:

The Show function of the Visual Basic MessageBox class.

This tells you that there are 20 overloads for the Show function. In other words, someone has set up 20 different versions of Show, just like we have two different versions of DisplayMessage.

A custom overloaded Subroutine in Visual Basic .NET

To use your new Sub, add this line of code to your button:

SimpleStaticClass.DisplayMessage("Kenny")

Run your code and see what happens.


OK, enough about overloads. Let's now create a proper class, one that can hold its head up in society without everyone pointing and laughing.

Back to the VB NET Contents Page

 


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk