Solved

Modules and classes ??

Posted on 2006-07-24
2
269 Views
Last Modified: 2010-04-23
Hi,

I would like to know about modules and classes, and why they are used and whether it is better to use one or the other.

I have read on a forum elsewhere that modules are no longer required, and everything is better being done in classes.

Also, if I use a classes, how would I reference this in my main code, is there a call option like in php (include something.php).

thanks
0
Comment
Question by:nickmarshall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 250 total points
ID: 17169363
In my opinion, Microsoft has gone away from modules in .net.  You still see them when you create a console application but other than that everything else is pointed towards classes.  All a module in .net is is a special class that everything inside by default is shared.  I personally don't like modules and prefer to use classes.  Here is an example of how you would use your class in code.

Sample Class

Public Class  MyClass
   public MyConnectionString as String = "Hello World"
End Class

In your forms or anyplace else you want to call the code you just create an instance of the class and call the property or method.

Dim _myClass as new MyClass
_myClass.MyConnectionString = "Goodbye"


If you want to create  a class that acts as a module you would just add shared to the properties and methods inside of the class

So
Public Class MyClass
   public Shared MyConnectionString as String = "HelloWorld"
End Class

Then you would access this by just calling     MyClass.MyConnectionString.  

Of course a property in a module would just be   MyConnectionstring and be global.  I find this to be a little less organized then having the item referenced in a class.  

This is just my opinion on the subject.  I am sure some instances may require a module but I just prefer the use of classes.  Hope that helps.
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17170102

Not to over-simplify, but you can think of them as follows :


A class is an object that you create instances of, holds code and only allows interaction through the methods/functions/subs you define. You interact with the instance you create to do things and "destroy" it when you are done.

Example :
Public MyNewClass
  public function PrintStuff(byval Stuff as string)
     ' Do things
  end functoin
 
  Public function CopyFiles (byval Driveone as string, byval DriveTwo as string)
    ' copy files here
 end function
end class

To use the above class :

   Dim Cr as new MyNewClass
      Cr.printstuff("this is my stuff!")
      ' Later in the code
      Cr.CopyFiles ("c:\", "d:\")
      ' later in the code
      cr = nothing ' Destroy instance of class, no longer needed.


A module is simply a "storehouse" for code. You don't create an instance of it, and therefore don't "destroy" it.
Modules are used for logical grouping of functions/subs/variables/etc that can be global to an application, a stand-alone .dll, etc etc.

You could create a .DLL that exposes the functions above, and say 50 more functions. In the project for this .DLL you decide it is easier to read if you create a module and put all of your variables in this module that are global to the class.


Another way to look at it :

If you have a single function that returns a formatted string value, then it would be a waste of resources to put this single function into a class, instantantiate it (create an instance of the class), call that instance and then destroy it.

If you have many data-access functions, supporting code/etc, then that is probably a good candidate to "wrap" inside of a class and call throughout your code.



As a summary :

A class is an object that you create instances of, holds code and only allows interaction through the methods/functions/subs you define. You interact with the instance you create to do things and "destroy" it when you are done.

A module is simply a "storehouse" for code. You don't create an instance of it, and therefore don't "destroy" it.
Modules are used for logical grouping of functions/subs/variables/etc that can be global to an application, a stand-alone .dll, etc etc
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question