Solved

Modules and classes ??

Posted on 2006-07-24
2
259 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now