Cor,
| Therefore I am in doubt now if the module used in this format is not a
| better choice than the Shared Class.
I agree if you "make a point" to qualify the module member's name I would
stay they are equal.
Unfortunately the IDE does not require you to qualify the module member's
name, nor does it require you to import the module name. This ease of
unqualified access of a Module's members is why I consider a shared class
is
"better".
Yes you need take extra steps to define the shared class. However
generally
you would define the class once, then use it a plethora of times. Its the
use a plethora of times where the benefit of the shared class over a
module
is. When you prevent instantiating & inheriting a shared class, the
compiler
will tell you if you forget to share one of its members...
Ideally I would prefer that VB introduce the equivalent of C#'s "static
class", a class that requires all shared methods and has no constructor.
Basically a Module that does not include the
Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute in the
generated IL. As I understand that the StandardModuleAttribute is what is
causing the implicit import.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
| Jay,
|
| Basically I don't agree with you in this because that written modules as
I
| have seen them are mostly used as an unordered bunch of fields. I hate
that,
| but before answering you I took this time the effort by trying some
things.
|
| That made that I came by this.
|
| Public Module GiveMySin
| Public Function MyFunction() As Double
| Return Sin(90)
| End Function
| End Module
| Public Class GiveMyCos
| Public Shared Function MyFunction() As Double
| Return Cos(90)
| End Function
| End Class
|
| I can call those by
|
| Dim a As Double = GiveMySin.MyFunction
| Dim b As Double = GiveMyCos.MyFunction
|
| I can use in both intellisence. Used as this however, does the module
| protect me from forgetting to write the shared key word or in other
words
| don't make it even necessary to write that..
|
| Therefore I am in doubt now if the module used in this format is not a
| better choice than the Shared Class.
|
| Or do you see that I miss something?
|
|
|
| Cor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <
[email protected]> schreef
in
| bericht | > Bonzol,
| > In addition to the other Comments.
| >
| > A Module is implicitly imported into every source file (effectively at
the
| > project level). This cannot be turned off.
| >
| > A Class with Shared methods (such as System.Math) needs to be
explicitly
| > imported into every source file or at the project level.
| >
| > Importing a class with shared methods allows to control if you want
the
| > members qualified or not. For example:
| >
| > Imports System.Math
| >
| > Public Module MainModule
| >
| > Public Sub Main()
| > Dim d As Double = Sin(90)
| > End Sub
| >
| > End Module
| >
| > verses:
| >
| > Public Module MainModule
| >
| > Public Sub Main()
| > Dim d As Double = Math.Sin(90)
| > End Sub
| >
| > End Module
| >
| >
| > NOTE: When defining a class with only shared methods. I normally make
it
| > Notinheritable with a private constructor. This prevents other
developers
| > from creating an instance of the class and prevents inheriting from
the
| > class.
| >
| > Public NotInheritable Class Math
| >
| > Private Sub New
| > End Sub
| >
| > Public Shared Function Sin(value As Double) As Double
| > ...
| > End Function
| >
| > End Class
| >
| > A Module (or static class in C# 2.0) simply doesn't have the
constructor.
| > CLI/C++
| >
| > I find both classes with shared method & Modules beneficial in a fully
OOP
| > program. For example truly "global" functions such as System.Math or
my
| > Generic IIF don't really fit in an OO world, and lend themselves well
to
| > Modules... Normally I put utility/helper methods in a class with
shared
| > methods, for example methods that need to be used from both Pages &
| > Controls
| > in ASP.NET while instantiating a class doesn't really bring any value
to
| > the
| > solution...
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley -
http://www.tsbradley.net
| >
| >
| > | > | vb.net
| > |
| > | Hey there, could someone just tell me what the differnce is between
| > | classes and modules and when each one would be used compared to the
| > | other?
| > |
| > | Any help would be great
| > |
| > | Thanx in advance
| > |
| >
| >
|
|