Sunday, November 2, 2008

Mon Nov. 3: Using Modules

Hey there hi there ho there,

Today, before you get onto working with your projects some more, you will need to do three things:
A) Create a module
B) Create a function
C) connect a button to some functions and make it do stuff!

Oh, OK, so you wanna actually KNOW what those two things are? Well, let's start with the second one. A function is just some code that makes things happen. It's kind of like the events that we've been using, events like "keydown" or "buttonclick"EXCEPTTTTTTTTTT that functions don't belong to any particular control. they can just kind of sit around all by themselves and wait to be used by any control you say that they should be used by. Which brings us to:

MODULES. A module is just a place where you can store functions. These functions can be called by any control or any form within your program.

Alllllllllllllllllllllllllllllllllllllllllll RIGHTY then, let's launch into this shall we?

What to do

A) start up a new project, and name it after yourself, plus the word "functions"

B) In "Form1" please create two buttons and five labels. Replace the text of one button with this text "MessageBox Function"

C) Replace the text of the other button with this "Call Many functions"

D) Create a module by clicking on the Project menu, then clicking "Add Module". This will add a tab called "Module1.vb" at the top of your form.

E) Click on that tab if necessary, and add this code right below where it says "Module Module 1"

Public Function AddRandom() As Integer 'declaring a function that adds integers
Dim x, y As Integer
'declaring two integers and assigning values to them
x = Int(Rnd( ) * 10) ' a random number
y = Int(Rnd( ) * 20) 'another random number
'adding two integers and return the value
Return (x + y)
End Function

F) Get back into the main form, and doubleclick on the button that says "MessageBox Function"

G) add this to its code:
MsgBox(AddRandom())

Run the program to make sure it works. When everyone is set, I will pass out the code sheets to make a function that calls other functions.

When everyone is done with that, you will upload this new program to the "VBdepot" folder on Box.net, and then proceed to working with your projects

Cheers,
Mr. L

No comments: