Which do you prefer?
What are the disadvantages of using module or Class and there advantages?
some vb6 programmers prefer module and some not.
Printable View
Which do you prefer?
What are the disadvantages of using module or Class and there advantages?
some vb6 programmers prefer module and some not.
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.
Hi
I use the module to declare constants, structures and public vars. I use classes for everything else.
Regards
Jorge
I don't use modules. :(
Hi,
Under VB.Net, the module is actually a class where everything's shared (static). So:
is the same as:VB Code:
Module myModule Public globalVar As String Public Sub doIt() globalVar = "" End Sub End Module
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 :afrog: in the name of da Lord is THAT ?:afrog:". For some project types, however, the module is the only way to go (like console apps).
Cheers,
NTG
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.
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.
Yeah, but you don't really need to instansiate an object of a class with only shared members.Quote:
Originally posted by TalonSoftware
But can you instanciate an instance of the module? Somehow I think that's the biggest diff between the two.
Right.Quote:
Originally posted by RickP
Plus inheritance and polymorphism.
Cheers,
NTG
Right :)
The statement:
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.Quote:
Under VB.Net, the module is actually a class where everything's shared
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:
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.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
Cheers,
NTG
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.