Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between Modules, Classes and Components? 1

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
What is the difference between a Module, Class and Component? They all seem similar to me. I have worked with Modules in VBA before, now that I am into ASP an VB I have Classes and Components as well.

I am attempting to build 2 functions. 1 to populate objects, the other to execute nonquerys. The would reduce the amount of code I have to write per control.

I tried a component class and that didn't work when I attempted to call the pulbic function, I tried a class and that did not work when I attempted to call the function. I finally tried a Module and now I can call the function and pass the variables and everything works.

Why is that though? Thank you in advance.

Jason Meckley
Database Analyst
WITF
 
The difference between modules and classes is that classes are designed to be instantiated at run time, by using the new keyword. Then, the instance of that class (ie the object derived from the class) maintains its own state until it is disposed of. Modules just contain code, for example functions and subs you want to use elsewhere in your app, and you don't need to get an instance before you can call the code in the module. So they are ideal for stuff where the code does not rely on state.

There is no real difference between a component class and a regular one. A component class simply provides a "surface" in VS.net, where you can visually design controls it relies on (for example db connections, timers, etc). But you could do the same thing in code in a regular class.

Hope this helps.



Mark [openup]
 
Hey Jason:

Module: code file containing code that can be accessible without creating an instance of an object

Class: construct containing code that must be created first before access can be granted

For instance, if I have a function called MyFunc():

If I put this in a module, and declare the function public, I can access it anywhere in my project by simply saying:
MyFunc()

If I put this within a class, and declare the function public, then I must first create an instance of the class.

Dim myClass as new Class
myClass.MyFunc()

Now how you use these can be a religious discussion, depending on what your development methodology states.

For instance, for the two functions you're describing, I'd have an object coded to hold information for a given class. However, I'd have a seperate set of functions (could be in a module, I prefer to object everything though) to actually communicate to the database.

So if I had a Client object, and wanted to fill/insert/update/delete:

=============================
Object:Client
-----------------------------
Properties:
ID
Name
-----------------------------
Functions:
FillObject()
UpdateObject()
DeleteObject()
==============================

Then in a seperate module or class, you could create:

==============================
Module: DataTier
------------------------------
Functions:
ExecNonQuery()
ExecScaler()
etc.
==============================

So in your object, you just call the functions you need as far as data access goes (in my apps, I pass them a command object, and it takes care of the connection)

Anyway, hope this helps, and let me know if I need to clarify anything.
:)

D'Arcy

 
Thanx for your help. I decided to make these functions within a class. I don't know why, but when the functions where within a module errors would occur that caught me in a loop and froze my computer when I would return to the page a second time. I couldn't even use the debugger to stop. I changed the module to a class and now I do not have the infinite loop error.

Jason Meckley
Database Analyst
WITF
 
Yes - a module is always shared.
You could stick the methods in a class, and declare them all shared, or you could declare the class shared, and it would essentially be the same thing as a module.

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top