|
-
Oct 18th, 2004, 07:49 PM
#1
Thread Starter
Frenzied Member
Module or Class?
Which do you prefer?
What are the disadvantages of using module or Class and there advantages?
some vb6 programmers prefer module and some not.
-
Oct 18th, 2004, 07:56 PM
#2
Lively Member
They are completely different. Modules don't have the features clases do, like inheritance or polymorphism. They can't be compared.
If you are talking about using a Sub Main then I say module since it makes things easier to read.
-
Oct 19th, 2004, 03:00 AM
#3
Hi
I use the module to declare constants, structures and public vars. I use classes for everything else.
Regards
Jorge
"The dark side clouds everything. Impossible to see the future is."
-
Oct 19th, 2004, 04:56 AM
#4
Fanatic Member
I don't use modules.
-
Oct 19th, 2004, 11:04 AM
#5
Hi,
Under VB.Net, the module is actually a class where everything's shared (static). So:
VB Code:
Module myModule
Public globalVar As String
Public Sub doIt()
globalVar = ""
End Sub
End Module
is the same as:
VB Code:
Public Class myClass
Public Shared globalVar As String
Public Shared Sub doIt()
globalVar = ""
End Sub
End Class
Therefore, you can use modules classes with shared members. However, the readability factor is different and it depends on your background - a VB programmer would be comfortable with modules but a C# programmer will say "What in the name of da Lord is THAT ? ". For some project types, however, the module is the only way to go (like console apps).
Cheers,
NTG
-
Oct 19th, 2004, 12:46 PM
#6
Lively Member
But can you instanciate an instance of the module? Somehow I think that's the biggest diff between the two. With a module, you are going to have one shared copy of it's contents, while with a class, you can have multiple, independant copies of the object.
-
Oct 19th, 2004, 01:54 PM
#7
Lively Member
Plus inheritance and polymorphism. This is why you can't really compare the 2. This is 2 of the 3 reasons a class even exists.
-
Oct 19th, 2004, 04:15 PM
#8
Originally posted by TalonSoftware
But can you instanciate an instance of the module? Somehow I think that's the biggest diff between the two.
Yeah, but you don't really need to instansiate an object of a class with only shared members.
Originally posted by RickP
Plus inheritance and polymorphism.
Right.
Cheers,
NTG
-
Oct 19th, 2004, 04:20 PM
#9
Lively Member
Right 
The statement:
Under VB.Net, the module is actually a class where everything's shared
Is not true due to the fact of a modules limited support for class features. Therefore you really can't compare the 2, or the question needs to be more presice.
-
Oct 19th, 2004, 04:42 PM
#10
Agree about limited class features, but it's a class alright - more accurately, it's a sealed shared class derived from System.Object. Also, if you don't declare the module public, it's a private class. If you have a forms project with the module:
VB Code:
Module myModule
Public globalVar As String
Public Sub doIt()
globalVar = ""
End Sub
End Module
then build the DLL and open it with ILDasm, so you'll see something like this:
Code:
.class private auto ansi sealed myModule
extends [mscorlib]System.Object
{
.custom instance void [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute::.ctor() = ( 01 00 00 00 )
.field public static string globalVar
.method public static void doIt() cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr ""
IL_0006: stsfld string WindowsApplication14.myModule::globalVar
IL_000b: nop
IL_000c: ret
} // end of method myModule::doIt
} // end of class myModule
Moreover, if you declare a module as public in a class project and you create the DLL, you can actually use the module exactly as you would use a class with shared members from a C# project. Still you can't inherit from it but that's because the module is also a sealed class.
Cheers,
NTG
-
Oct 20th, 2004, 07:21 AM
#11
Lively Member
It may treat it as a class underneath, but in any OOL, certain things must be met to be classifed as a class. And a module doesn't support these features, so it really isn't a class from the programmers point of view. But it's nice to see what's happening underneath.
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
|