Class in a module or class in a class?

C

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
 
M

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
 
C

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.
 
A

Armin Zingler

cj said:
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
 
M

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top