The different between "Class Moudle" and "Moudle"?
EmilMatthewI have seen some source code which were coded in VB with DirectX. I can see the programmer use both "Visual Basic Class Moudle" and "Visual Basic Moudle",but what had confused me was that ,I didn't know the difference between the two. Because as I thought,the code to be used in "Class Moudle" and "Moudle" was seen similar. I didn't know much about what kind of function could only be declared in "Class Moudle" ,and what kind of function could be only used in "Moudle". Thanks a lot!
VBBRAny kind of function can be defined and used in both. The difference is, in a normal Module, you call the functions and variables directly (for example, "Result = CalculateResult(2, 4)", if you defined the CalculateResult function in some normal module. You can call this from anywhere in your program assuming the function is defined as Public. If you only want the function to be accessed from within the module in which it was defined, define it as Private. The same applies to variables.) A class module is an object. Being so you need to define all functions and variables of it in its code, then, in another module define a instance of it, like this: (suppose you have a class module called Math) Dim Ma1 as Math Set Ma1 = New math See? Then you can access the functions and variables inside Math through the instance called Ma1 (or whatever you decide to call it) this way: Result = Ma1.CalculateResult(2, 4) It's really simpler than it looks. This doesn't apply just to VB, it's a very common OO (object orientation) concept, and probably the most basic one. A Class Module is the same thing as a Class. (class is the most common name used in all programming languages)
sdwA Module is used to declare global things like variables, functions, and user defined types (plus more) which are then able to be used throughout the entire project. A class is kind of like a user defined type (a group of variables), only with its own functions. After you've built your class module you need to make a new instance of it in order to use it. You can do this by two means known to me: [code] 'Say we named our class module to clsSomeObj '1st way: Dim myObject as new clsSomeObj '2nd way: Dim myObject as clsSomeObj Set myObject = new clsSomeObj [/code] Hope that helped you. Go back to that sourcecode you have and look at how the programmer has used the Modules and Classes. BTW - it's not a "Moudle" it's a "Module" :)
SionI think you talking about Modules - not "Moudle". The main difference is that classes (Class Modules) can be used create different instances from, while a Module is static and dosn't work the way objects, controls and classes do. A class can work much like a Command Button; It can have properties, events, private and public functions and subs, and once you have built a class you can use it elsewhere in you code by creating instances of it. A module dosn't need to be initialized, so it's not possible to have module-objects. Modules can also have properties, events, private and public functions and subs as well as global variables that is available from anywhere in the project. Classes are the key to Object Oriented Programming (encapsulation etc.) and can be used to imitate real-life objects, and modules are used to store repetative/general and global code.
EmilMatthewThank you so much ,I learned so much!