Modules

wild wolf
03-16-2002, 11:11 AM
I have been using VB now for 6 months (or less i think) and i still dont know the importance of using modules and what difference they make to a program.

Are they similar to header files in C?

Squirm
03-16-2002, 11:17 AM
A module is just a load of code.
It stores functions you need to use throughout the program.

It sometimes better than a form because a form has to be loaded before its functions can be used. Its sometimes better than a class because a class has to be declared and referenced. Modules just represent general code. Almost all my projects are based on modules.

I also like modules because you place common functions in a module and then plug than module into any project you need the functions.

:)

wild wolf
03-16-2002, 11:28 AM
Fine that is clear

one more question, you said that in a form, it is loaded first and then the functions, what about in a modules, does it load the whole module at a go or just loads the functions when they are needed.

how do you use them by the way?

Squirm
03-16-2002, 11:35 AM
Modules are not loaded or created. They are just 'there', and any subs and functions within in them declared as Public can be used from anywhere within the program at any time.

:D

In a module:

Public Function Hello() As String
Hello = "World"
'Cheezy I know
End Function

In a form:

Private Sub Command1_Click()
Dim sHello As String
sHello = Hello 'Can be used like this
sHello = Module1.Hello 'Or like this
'(if multiple modules have functions with the same name)
MsgBox sHello
End Sub

Kezman
03-18-2002, 03:56 AM
can i declare a public variable here ?
how do i access it then if possible?

Pookie
03-18-2002, 04:28 AM
You can only declare a public variable in a module (or class) but not with forms.

You can access a public variable from anywhere in the code, be it form, module, etc.

You just declare the variable at the start of a module.


Option Explicit
Public myVariable as integer

ChiefRedBull
03-18-2002, 06:43 AM
You can only declare a public variable in a module (or class) but not with formsNot true. You can declare Public variables within forms, then access them as
Form.VariableName.

Osiris
03-18-2002, 12:28 PM
Private Sub Command1_Click()
Dim sHello As String
sHello = Hello 'Can be used like this
sHello = Module1.Hello 'Or like this
'(if multiple modules have functions with the same name)
MsgBox sHello
End Sub


i thought that if multiple mudules have functions with the same name it would cause an error?

Iceplug
03-18-2002, 03:58 PM
It shouldn't create an error.
If two functions are identical within the same module it will cause an error. If two functions are identical, but in different modules, they can be referenced seperately by putting the name of the module, ".", and the name of the functions.
Module1.StringCut(^^^^^, ^^^^^)
Module2.StringCut(^^^^^)
Something like this.:)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum