Home All Groups Group Topic Archive Search About

Class in a module or class in a class?

Author
3 Mar 2006 9:20 PM
cj
I've got some classes for a program and the classes were written in
Module1.  It works fine however I am curious if instead of doing
"project|add module" I'd done "project|add class" how is that different?

Here is the module I added.  Note it contains 2 classes and the sub main.

Module Module1

     <STAThread()> Public Sub main(ByVal CmdArgs() As String)
         Dim mainForm As New Form1
         Application.Run(mainForm)
     End Sub

     Public Class MyThreadCount

         Private Shared m_lock As New Object
         Private Shared m_threadcount As Int32 = 0

         Public Shared Sub Increment()
             SyncLock (m_lock)
                 m_threadcount += 1
             End SyncLock
         End Sub

         Public Shared Sub Decrement()
             SyncLock (m_lock)
                 m_threadcount -= 1
             End SyncLock
         End Sub

         Public Shared ReadOnly Property ThreadCount() As Int32
             Get
                 Dim _count As Int32
                 SyncLock (m_lock)
                     _count = m_threadcount
                 End SyncLock
                 Return _count
             End Get
         End Property
     End Class

     Public Class MyStringLogger
         Private Shared m_loglock As New Object

         Public Shared Sub Write(ByVal str As String)
             SyncLock (m_loglock)
                 Dim sw As New System.io.StreamWriter("c:\validate.log",
True)
                 sw.WriteLine(str)
                 sw.Close()
             End SyncLock
         End Sub
     End Class
End Module

Author
3 Mar 2006 9:40 PM
Mattias Sjögren
>It works fine however I am curious if instead of doing
>"project|add module" I'd done "project|add class" how is that different?

It wouldn't change anything in this example. A module is really just a
special kind of class with some restrictions (all members are
implicitly Shared, the type can't be instantiated).

I'm curious why you nest your classes in the module? You don't have to
put your classes (MyThreadCount et al) inside another module or class
unless you really want to.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
3 Mar 2006 10:01 PM
cj
You mean as opposed to putting them in Form1.vb after "end class" for
"public class form1"?  If so the answer is hadn't really thought about
it much, but I kinda like having them more separated from the events of
form1.


Mattias Sjögren wrote:
Show quoteHide quote
>> It works fine however I am curious if instead of doing
>> "project|add module" I'd done "project|add class" how is that different?
>
> It wouldn't change anything in this example. A module is really just a
> special kind of class with some restrictions (all members are
> implicitly Shared, the type can't be instantiated).
>
> I'm curious why you nest your classes in the module? You don't have to
> put your classes (MyThreadCount et al) inside another module or class
> unless you really want to.
>
>
> Mattias
>
Author
3 Mar 2006 10:29 PM
Mattias Sjögren
>You mean as opposed to putting them in Form1.vb after "end class" for
>"public class form1"?

Well that's one option, but personally I try to stick to a single
class or module per file. In other words, create separate code files
MyThreadCount.vb, MyStringLogger.vb and so on. The compiler doesn't
care which file you put them in but it makes the project files easier
to find and maintain for you.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
3 Mar 2006 10:10 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> I've got some classes for a program and the classes were written in
> Module1.  It works fine however I am curious if instead of doing
> "project|add module" I'd done "project|add class" how is that
> different?

The difference is the location of the classes. Put in a Module, they are
nested classes within the outer class, thus the FQN is
"ProjetRoot.Module1+MyThreadCount". Not put in a Module, they are part of
the root namespace "ProjectRoot.MyThreadCount". In a Module, you can also
declare them Private.


Armin