Modules or Class modules?
Author |
Message |
John Rel #1 / 4
|
 Modules or Class modules?
Hello, This is not a very important question, but I put it anyway: At the launch of my application, I wish to load the language from a text file. The code which does this is long and bulky - mostly repetition. Do I put this in a class module, load my setting and destroy the class? Or do I just drop it in a normal module? Is there even a difference? -John.
|
Sun, 02 Feb 2003 06:42:02 GMT |
|
 |
Steve Gabrilowit #2 / 4
|
 Modules or Class modules?
Quote:
> Hello, > This is not a very important question, but I put it anyway: At the launch of > my application, I wish to load the language from a text file. The code which > does this is long and bulky - mostly repetition. Do I put this in a class > module, load my setting and destroy the class? Or do I just drop it in a > normal module? > Is there even a difference? > -John.
HUGE difference John! A class module is in a way not really even a module, but rather a template for a module, and for each time you use it you have to create at least one instance of it. So why does this matter? It depends on the way your software is going to use the module. To use your language question as an example (and not a very good one) if you call the routines within a module with a parameter (like the name of the language file) and specify that parameter every time you use one of the routines. On the other hand, if you invoke the methods of an object (an instance of the template defined in the class module) they can "know" what language file to use (the code is shared but each instance has its own copy of the data storage area. For the implementation you seem to be planning, I think you are looking at a regular module. Now say, on the other hand, that you are writing a game, maybe with multiple spaceships fighting each other. Every spaceship has its own characteristics, strength, damage, location, etc etc but every one of them behaves in the same way given the same variables apply. This is where object oriented programming comes into play! I'm sure you've heard that buzzword before, but maybe didn't realize that class modules are how the object oriented features of VB are implemented! The beauty of this approach is that once you write the code for one of the ships you have the code for ALL of them. now you can simply create an array to hold the ships and then let them fly! OK, this is simplified a bit and there are some subtleties you need to learn, but at least you get the general idea.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
John Rel #3 / 4
|
 Modules or Class modules?
... Quote: > HUGE difference John! A class module is in a way not really even a module, but > rather a template for a module, and for each time you use it you have to create > at least one instance of it. So why does this matter? It depends on the way > your software is going to use the module. To use your language question as an > example (and not a very good one) if you call the routines within a module with > a parameter (like the name of the language file) and specify that parameter > every time you use one of the routines. On the other hand, if you invoke the > methods of an object (an instance of the template defined in the class module) > they can "know" what language file to use (the code is shared but each instance > has its own copy of the data storage area. For the implementation you seem to > be planning, I think you are looking at a regular module. > Now say, on the other hand, that you are writing a game, maybe with multiple > spaceships fighting each other. Every spaceship has its own characteristics, > strength, damage, location, etc etc but every one of them behaves in the same > way given the same variables apply. This is where object oriented programming > comes into play! I'm sure you've heard that buzzword before, but maybe didn't > realize that class modules are how the object oriented features of VB are > implemented! The beauty of this approach is that once you write the code for > one of the ships you have the code for ALL of them. now you can simply create > an array to hold the ships and then let them fly! > OK, this is simplified a bit and there are some subtleties you need to learn, > but at least you get the general idea.
.... Thanks a lot. I did not feel like having more than 5 modules in my project, so figured that a class module would look neater, since it is tucked away in the corner. Actually, I plan to load the language during launch - meaning that I only call the function once. It could be that functions in modules use memory in some way, and that class modules - since they are created and destroyed - do not do this. -John.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
J Fren #4 / 4
|
 Modules or Class modules?
You have a large chunk of Code that reads in a load of Text, the code is then redundant, and the text could be held anywhere. Would using a Class instead of a Module save you memory ? Basically you are asking whether destroying a Class unloads its *Code* space. It does not. However if you put the Code in an ActiveX DLL then you could destroy all references to the DLL and then it would (if necessary) be unloaded. You could be *certain* of this - and control it by putting the code in an ActiveX EXE Personally I would do this as you could set the ActiveX EXE to startup from Sub Main, Shell to it (to ensure it is registered), create an instance requesting the data - this would just set up a flag in the EXE and return, then continue with setup, when it is completed (and you are ready) take the data - and tell it to die. That way you will have created a second Thread to handle the reading of the data, you can test it standalone, be certain of killing it, and concentrate on Screen Activity while a processing intensive task takes place.
Quote: >Hello, >This is not a very important question, but I put it anyway: At the launch of >my application, I wish to load the language from a text file. The code which >does this is long and bulky - mostly repetition. Do I put this in a class >module, load my setting and destroy the class? Or do I just drop it in a >normal module? >Is there even a difference? >-John.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
|