Re: Shared classes or modules?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 02/07/04


Date: Sat, 7 Feb 2004 12:27:46 -0600

Erik,
Remember that in a Module all members are Shared implicitly, so for a module
you do not specify Shared.

    Public Module Module1

        Sub New()
        End Sub

    End Module

Is the same as:

    Public Class Class1

        Public Shared Sub New()
        End Sub

    End Class

The biggest advantage of a constructor in a Module is to contain
initialization code that is executed the very first time you use any member
of the Module. Which enables Lazy Initialization with little or no code from
you.

For example, if I had a LogModule that had methods to write items to a log
file. I could have the Sub New above open the log file the first time I went
to write to the file. When using methods of the module subsequently the log
file itself would already be open.

    Imports

    Public Module LogModule

        Private m_stream As StreamWriter

        Sub New()
            m_stream = New StreamWriter("mylog.log")
        End Sub

        Public Sub Write(ByVal text As String)
            m_stream.Write(text)
        End Sub

    End Module

The above also shows why I tend to prefer "Shared" Classes over Modules, the
Write method is ambiguous (yes I could have called it WriteLog, however
WriteLog is not as encapsulated...)

Hope this helps
Jay

"Erik Cruz" <erikacf.spammers.out@antares.com.br> wrote in message
news:%23kaQTwZ7DHA.2576@TK2MSFTNGP11.phx.gbl...
> Hi Jay.
>
> Now I understand it better, thanks. I have another question if you don't
> mind. Other posts say that a module have no default constructor but it can
> have a shared one if needed. If I declare a Public Shared Sub New() in my
> module I get an error. How can a shared constructor be created on a
module?
> When is it useful?
>
> Thanks again for your time.
>
> Erik
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
> news:eVIIyNG7DHA.2392@TK2MSFTNGP11.phx.gbl...
> > Erik,
> > In addition to Rob's comments about a "Shared" class is a class that
only
> > has Shared members (all methods, properties, fields etc, are shared).
> >
> > Also you should make the constructor private, to prevent instantiating
an
> > instance of the class and make the Class Notinheritable to prevent
> deriving
> > from the class.
> >
> > A Module actually does not have a constructor, so you are preventing
from
> > instantiating it. And a Module is Notinheritable to prevent deriving
from
> > it.
> >
> > I normally prefer "Shared" classes over Modules as they require the
class
> > name to prefix the member names, which to me is better encapsulation
(you
> > know where that identifier is coming from). However "Modules" are useful
> for
> > truly "global" functions, such as Math function.
> >
> > You can use Imports on a class name so the class name is not required on
> > shared members. Such as:
> >
> > Imports System.Math
> >
> > Hope this helps
> > Jay
> >
> > "Erik Cruz" <erikacf.spammers.out@antares.com.br> wrote in message
> > news:eOOsX5F7DHA.3704@tk2msftngp13.phx.gbl...
> > > Hello!
> > >
> > > I have read some threads discussing the fact that a module is in
reality
> a
> > > shared class. If I try to create a Public Shared Class in vb.net I
> receive
> > a
> > > compile error. Why? If I can't explicitly create a shared class, how
> does
> > > vb.net creates it?
> > >
> > > TIA,
> > > Erik Cruz
> > >
> > >
> >
> >
>
>



Relevant Pages

  • Re: C++ 101 dumb question
    ... Its action is to copy the data members of the class, ... If one of those members is a pointer, ... have it's own copy constructor would the default copy constructor call ... as a copy of the class being returned is placed on the stack for the ...
    (microsoft.public.vc.language)
  • Re: Error using ==> class
    ... The argv in there is obviously a holdover. ... varargin because varargin apparently does not actually allow nargin == ... the change, no changes to code are made, and the constructor (as seen ... All instances of an object must have the same member names in the same ...
    (comp.soft-sys.matlab)
  • Re: DbC & Exceptions & Style
    ... There is insufficient problem space context to critique the class. ... FWIW, though, I don't see a whole lot of DbC here if one relies on ... I don't like this because it complicates the constructor. ... Therefore whoever does the instantiating has to understand ...
    (comp.object)
  • Re: ECMAScript standard:the code(s) for figure on page 3 ?
    ... You need to assign that object to the public prototype property of the ... assigned a reference to the empty anonymous function on the right hand ... Instance have property from constructor. ... member create from scope of constructor function. ...
    (comp.lang.javascript)
  • Re: composition/aggregation: would like to use constructor body rather than initializer list
    ... "constructor chaining" takes in C++; this type of mechanism is essential ... and ubiquitous (whether you're dealing with pointers or not) across all OO ... >the constructor or get some non-trivial input info, before building member ... The contemporary wisdom in C++ as it applies to initialization lists is ...
    (alt.comp.lang.learn.c-cpp)