|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Class in a module or class in a class?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 >It works fine however I am curious if instead of doing It wouldn't change anything in this example. A module is really just a>"project|add module" I'd done "project|add class" how is that different? 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. 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 > >You mean as opposed to putting them in Form1.vb after "end class" for Well that's one option, but personally I try to stick to a single>"public class form1"? 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. "cj" <cj@nospam.nospam> schrieb The difference is the location of the classes. Put in a Module, they are > 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? 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
USB Pen Drive Detection - Retrieving
Dates are Evil! HELP! What's going on in this ADO2 code Countdown/pause/resume timer Cannot add MSWord reference Testing simple VB code in design time? Adding New member to TextBox.. using a integer variable with a vb.net datetime literal how linkitem and linkTopic to migrate in vb.net __declspec(dllexport) c++ |
|||||||||||||||||||||||