Results 1 to 11 of 11

Thread: Module or Class?

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Cool 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.

  2. #2
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    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.

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    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."

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    I don't use modules.

  5. #5
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449
    Hi,

    Under VB.Net, the module is actually a class where everything's shared (static). So:

    VB Code:
    1. Module myModule
    2.  
    3.     Public globalVar As String
    4.  
    5.     Public Sub doIt()
    6.  
    7.         globalVar = ""
    8.  
    9.     End Sub
    10.  
    11. End Module
    is the same as:
    VB Code:
    1. Public Class myClass
    2.  
    3.     Public Shared globalVar As String
    4.  
    5.     Public Shared Sub doIt()
    6.  
    7.         globalVar = ""
    8.  
    9.     End Sub
    10.  
    11. 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
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  6. #6
    Lively Member
    Join Date
    Sep 2004
    Posts
    96
    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.

  7. #7
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    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.

  8. #8
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449
    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
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  9. #9
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    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.

  10. #10
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449
    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:
    1. Module myModule
    2.  
    3.     Public globalVar As String
    4.  
    5.     Public Sub doIt()
    6.  
    7.         globalVar = ""
    8.  
    9.     End Sub
    10.  
    11. 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
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  11. #11
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    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
  •  



Click Here to Expand Forum to Full Width