|
-
Jul 16th, 2008, 02:07 AM
#1
Thread Starter
New Member
[2005] Class' & Modules
What's the difference between a class and a module?
Did I mention I love you all for answering my stupid questions?
I found this in google:
Code:
instances can be created from a class in the form of objects, but not from a module
What does it mean?
Thanks.
-
Jul 16th, 2008, 02:17 AM
#2
Re: [2005] Class' & Modules
When you write code in a class then you can create multiple copies of that class on demand. To use code of a class, you need to create instance of the class but that is not the case with Modules. Code written in Modules can be accessed from anywhere in the application.
-
Jul 16th, 2008, 02:38 AM
#3
Re: [2005] Class' & Modules
Similarities:
Both are collections of code and can contain methods - sub procedures (Sub...End Sub) or functions (Function...End Function) as well as variables - public, private etc.
Differences (purpose):
Modules are simply collections of code. Classes are collections of code which, when grouped together define an object.
If you take for instance a car (typically used to explain classes), in real life you can define certain items of a car which define it - number of doors, wheels, colour, sunroof, engine size etc. In coding, you take these same items but then write these into, and expose these from a class via the use of properites (which modules do not support), and if needed methods - like a Sub AccelerateBy10MPH()...End Sub. Once you have the definition created, you can use code similar to the following to use the defined object -
Code:
With myCarClass
.Colour = Red
.NumberOfWheels = 4
.HasSunroof = False
End With
As you might appreciate, this allows you to create several instances, in "instanciations" of the class and set the properties completely differently. I could write a myCarClass1 variable which represents a blue, 6 wheeled car, and a separate myCarClass2 which represents a red 3 door car.
When you write code using "form." or "button." or "textbox." at the moment, you are writing against objects which have been defined in the same way to the above car example. All these are classes too.
A step ahead from this simple sample, is that you can further child classes and use inheritance. This is slightly off-track but will help give you an understanding of the power of classes over modules. Taking the car class further, I could declare a LamboghiniCountash class and tell this that its related to my car class. I can choose to forceably define some of the properties - for example the Countash always has only 2 doors, but still expose some other properties from the base car class - such as a colour of the car can still be chosen. With these relationships and having a base class defining an overview Car, then child objects further tweaking the definition specifically (and adding it's own definitions), you have a robust, reuseable, structured programming design to use within your projects and this is one of the basis of Object Oriented Programming (or OOP for short).
Differences (declaration):
You can call an item (method or public variable) from a module by using similar to the following syntax in your coding:
Code:
ModuleName.MethodName
Classes however must be instanciated - as that result you found mentions. his means you first have to create an instance of the class in memory, and you need to use the New keyword to do so. The following coding would be a way to call the same method as used in the module sample code above, but from a class:
Code:
Dim myClass as new className
myClass = new ClassName
myClass.MethodName
Last edited by alex_read; Jul 16th, 2008 at 02:41 AM.
-
Jul 16th, 2008, 02:56 AM
#4
Re: [2005] Class' & Modules
The only real difference between classes and modules is that you cannot create an instance of a module. When you compile your code each module is actually implemented as a class.
VB classes can have Shared members that get called on the class itself rather than an instance of the class. Modules are like classes where every member is Shared. In fact, when you compile that's exactly what they are.
The only difference between a VB module and a C# static class, which is a class that MUST have every member declared static (equivalent of Shared) is that in C# you must qualify the member with class name in code. In VB you can choose to omit the module name when accessing a member. That's why you can call just MsgBox instead of Interaction.Msgbox. The MsgBox function is, in fact, a member of the Microsoft.VisualBasic.Interaction module.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|