Use of Modules

  • Thread starter Thread starter SamSpade
  • Start date Start date
S

SamSpade

I've used modules like I did in VB6, but is there any reason.

If I have a variable in a module because I want it to be available to all
instances could I instead put it the UserControl file and make it shared?
Is that the same?
 
SamSpade said:
I've used modules like I did in VB6, but is there any reason.

Only for simplification.
If I have a variable in a module because I want it to be available to
all instances could I instead put it the UserControl file and make it
shared? Is that the same?

Why Usercontrol? It would be the same if you put it in a class and import it
at project level:

Friend notinheritable class c
private sub new
end sub

public shared x as integer
end class

In addition, add <RootNamespaceOfProject>.c to the project imports and you
have the same as a Module. With Modules this is reached by adding the
Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
SameSpade,
In addition to Armin's comments.

Using Shared members of classes promotes encapsulation. In that you know
exactly where that Shared member is coming from.

For example:

Public Module SampleModule

Public Shared Function ActiveConnection As SqlConnection
End Function

End Module

Public Class SampleClass

Public Shared Function ActiveConnection As SqlConnection
End Function

End Class

You can use SampleModule.ActiveConnection without qualifying the name with
SampleModule. When you drop SampleModule, you are not certain where the
identifier is coming from.

However with SampleClass.ActiveConnection, you have to qualify the name,
ensuring that you know exactly where ActiveConnection is coming from.

As Armin suggested, you can import the SampleClass itself, allowing you to
use ActiveConnection unqualified. I normally reserve importing classes for
truly global functions, such as System.Math.

Hope this helps
Jay
 
Back
Top