Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modules Standard Modules. Standard Modules The Subs and Functions worked perfectly well where they were – inside the two lines “Public Class Form1” and.

Similar presentations


Presentation on theme: "Modules Standard Modules. Standard Modules The Subs and Functions worked perfectly well where they were – inside the two lines “Public Class Form1” and."— Presentation transcript:

1 Modules Standard Modules

2 Standard Modules 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.

3 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.

4 So start a new project. To add a Module to your project, click Project from the menu bar. From the menu, click on “Add Module”:

5 When you click “Add Module”, you’ll get the following 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.

6 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:

7 You can start writing your subs and functions

8 Now click back on your form and try to call the sub AddNumbers(10) 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:

9 The reason it can’t see it is we made the Sub Private. Only code inside of the modVerify Module can see a Private Sub. If you want the Sub or Function to be available to all the code in your project, you have to make then Public. This involves nothing more than changing the word Private to Public. Amend your Sub to this:

10 We’ll now add a Function to our Module.

11 The point about creating a Module to house all your Subs and Functions is that they are in a separate file. You could write more Subs and Functions for your Module, ones that validate text coming from a textbox (an email checker, for example, or one that uses the ProperCase string conversion). You would then have all this code in one file that you could add to totally different projects. If the Subs and Functions were in the same code for the Form, you would have to import the whole Form before you could use the very useful Subs and Functions you created.

12 StrConv( ) Inbuilt function that does converting. This is short for String Conversion. In between the round brackets, VB needs you to put two things: – the string you want to convert, – and what sort of conversion you want. As soon as you type a comma after the string you want to convert, VB pops up a box of available conversion types:


Download ppt "Modules Standard Modules. Standard Modules The Subs and Functions worked perfectly well where they were – inside the two lines “Public Class Form1” and."

Similar presentations


Ads by Google