Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Modules and classes ??

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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