473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Class in a module or class in a class?

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
Mar 3 '06 #1
4 1065
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.
Mar 3 '06 #2
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:
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

Mar 3 '06 #3
"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

Mar 3 '06 #4
>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.
Mar 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
0
by: Brano Zarnovican | last post by:
Hi ! I need to import a module and create an instance of a class from that module (in C). import mod o = mod.klass() (mod.klass is a subclass of tuple)
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
10
by: Bonzol | last post by:
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...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
6
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
3
by: Jeffrey Barish | last post by:
I have a class derived from string that is used in a pickle. In the new version of my program, I moved the module containing the definition of the class. Now the unpickle fails because it doesn't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.