Home and Learn: VB Net Course
This lesson is part of an ongoing tutorial. The first part is here: Create your own Subs in VB .NET
The Subs and Functions worked perfectly well where they were - inside the two lines "Public Class Form1" and "End Class". If you tried to put them on a line underneath End Class you would get lots of blue wiggly lines under your code.
That's because the code all belongs to Form1. But it doesn't have to. In fact, it's better to separate all your Functions and Subs and put them somewhere else - in something called a Module. We'll explore the Standard Module, and see how to move our Functions and Subs outside of Form1. That way, we can use them in other projects.
So start a new project. Add a button to you new form. To add a Module to your project, click Project from the menu bar. From the menu, click on Add Module:
When you click Add Module (or Add New Item), you'll see something like this dialogue box popping up:
Select Module from the Templates window. Type a name for your new module - modVerify.vb. When you've typed a name, click the Open button.
You'll should see a blank window, with this code in it:
If you take a look at the Solutions Explorer on the right, you should see that your new module is listed:
In between Module modVerify and End Module, type the following Subroutine:
Private Sub AddNumbers(ByVal num1 As Integer)
Dim answer As Integer
answer = num1 + 10
Your coding window should now look like this:
Now click back on your form and double click the button you added. This will bring up the code window for the Form, and the cursor will be flashing inside of the button code.
Add the following code for your button:
When you press the return key, you'll see a blue wiggly line under the Sub name. If you hold your mouse over AddNumbers, you might see this:
Because this lesson is a bit long, we'll continue it on the next page.
Buy the Book of this Course
Email us: enquiry at homeandlearn.co.uk