?
Solved

Modules and classes ??

Posted on 2006-07-24
2
Medium Priority
?
272 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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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