Solved

Modules and classes ??

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

13 Experts available now in Live!

Get 1:1 Help Now