473,396 Members | 1,895 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,396 software developers and data experts.

trying to understand classes

cj
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever. how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?

Feb 22 '06 #1
13 1288
cj
Maybe I can make this a bit clearer. I'm trying now to write a class
very much like this except it will have 1 sub which writes a line to a
txt file. The classes sub writeline will be called from withing many
concurrently running threads.

Questions in my mind:
1. Where do I open the file--I want it to stay open not be opened just
prior to each write. I think I'd want to do that in a constructor? Is
this right? Stephany's program doesn't seem to have a constructor. Yet
somehow it initializes m_threadcount to 0 only once.

2. If the object in Stephany's class, m_lock is used to lock the sub so
only one thread uses it at a time can be incrementing or decrementing
the count. Just for curiosity sake can my new class also have a object
called m_lock that would work separately from the one in mythreadcount?
I know I can name it something else. I'm trying to understand where
m_lock is visible. Can 2 classes have objects named m_lock and they not
interfere with each other?
cj wrote:
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever. how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?

Feb 22 '06 #2
HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe

"cj" <cj@nospam.nospam> wrote in message
news:uj*************@TK2MSFTNGP12.phx.gbl...
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever. how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?

Feb 22 '06 #3

"cj" <cj@nospam.nospam> wrote in message
news:uj*************@TK2MSFTNGP12.phx.gbl...
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean how
is it loaded I guess. why doesn't it need me to instigate it or whatever.
how does it know when to assign the variable m_threadcount to 0 on the
very first time a sub in the class is referenced?


Instance and Shared Variables:
http://msdn.microsoft.com/library/en...fvbspec7_5.asp

Mythran

Feb 22 '06 #4

"Joe Van Meer" <jv******@eastlink.ca> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe


Naw, it's a shared instance. Automatically created by the framework...I
posted a reply with a link that *should* explain it further. :)

Mythran

Feb 22 '06 #5
cj
That's what puzzles me. I didn't do that anywhere. It still works.
I'm thinking it's not an instance of the class I use but the class
itself and that bothers me too cause I just can't seem to understand. I
get stuff to work but really don't know why.

Joe Van Meer wrote:
HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe

"cj" <cj@nospam.nospam> wrote in message
news:uj*************@TK2MSFTNGP12.phx.gbl...
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever. how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?


Feb 22 '06 #6
cj
I'm afraid most links from MS don't make much sense to me. Sorry. I
take it .net makes the instance for me but when does it determine an
instance needs to be made?

Mythran wrote:

"Joe Van Meer" <jv******@eastlink.ca> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
HI there,

I imagine somewhere in that code is a line the instantiates your object
like:

Dim mythreadcount as New MyThreadCount

Or could be done in 2 steps, first is dimming it (making space in
memory),
then the actual instantiation of the object itself.

Dim mythreadcount as MyThreadCount
mythreadcount = New MyThreadCount

Hope that helps, Joe


Naw, it's a shared instance. Automatically created by the framework...I
posted a reply with a link that *should* explain it further. :)

Mythran

Feb 22 '06 #7
Actually, I think this is the link that explains why there is no need to
create an instance of the class:

http://msdn.microsoft.com/library/de...fvbspec7_1.asp

"Mythran" wrote:

"cj" <cj@nospam.nospam> wrote in message
news:uj*************@TK2MSFTNGP12.phx.gbl...
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean how
is it loaded I guess. why doesn't it need me to instigate it or whatever.
how does it know when to assign the variable m_threadcount to 0 on the
very first time a sub in the class is referenced?


Instance and Shared Variables:
http://msdn.microsoft.com/library/en...fvbspec7_5.asp

Mythran

Feb 22 '06 #8
Ooops, still wrong link :( Here is the correct one:

http://msdn.microsoft.com/library/de...BSpec7_1_5.asp

"TrtnJohn" wrote:
Actually, I think this is the link that explains why there is no need to
create an instance of the class:

http://msdn.microsoft.com/library/de...fvbspec7_1.asp

"Mythran" wrote:

"cj" <cj@nospam.nospam> wrote in message
news:uj*************@TK2MSFTNGP12.phx.gbl...
Stephany Young provided me with the following code to count threads
created by my program.

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

I use it by calling
mythreadcount.increment
mythreadcount.decrement
label1.text=mythreadcount.threadcount

Everything works great but I'm wondering how it gets started. I mean how
is it loaded I guess. why doesn't it need me to instigate it or whatever.
how does it know when to assign the variable m_threadcount to 0 on the
very first time a sub in the class is referenced?


Instance and Shared Variables:
http://msdn.microsoft.com/library/en...fvbspec7_5.asp

Mythran

Feb 23 '06 #9

cj wrote:
[snippage]
Stephany Young provided me with the following code to count threads
created by my program.

Public Class MyThreadCount

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

Public Shared Sub Increment() Public Shared Sub Decrement() Public Shared ReadOnly Property ThreadCount() As Int32 Everything works great but I'm wondering how it gets started. I mean
how is it loaded I guess. why doesn't it need me to instigate it or
whatever.
"Instantiate". And it doesn't need you to instantiate anything, because
all the variables and methods are Shared. I know you said you don't
really get links to the .NET documentation, but you really should take
the time to learn the difference between instance members (ones without
the Shared tag) and Shared members. It's pretty fundamental, and better
suited to being explained by a nice book or web page than by us in a
newsgroup post.
how does it know when to assign the variable m_threadcount to
0 on the very first time a sub in the class is referenced?
This line:
Private Shared m_threadcount As Int32 = 0


There can be some intricacies in how Shared member initialization
works, but for the most part it's safe to assume that it happens 'at
the start of the program'.

--
Larry Lard
Replies to group please

Feb 23 '06 #10

cj wrote:
Maybe I can make this a bit clearer. I'm trying now to write a class
very much like this except it will have 1 sub which writes a line to a
txt file. The classes sub writeline will be called from withing many
concurrently running threads.
If you're doing some kind of logging then there are mechanisms in the
Framework that will handle this better than any user code. See
<http://msdn.microsoft.com/library/en-us/vbcon/html/vbconIntroductionToInstrumentationTracing.asp>
= <http://tinyurl.com/zprec> for more.
Questions in my mind:
1. Where do I open the file--I want it to stay open not be opened just
prior to each write. I think I'd want to do that in a constructor? Is
this right? Stephany's program doesn't seem to have a constructor. Yet
somehow it initializes m_threadcount to 0 only once.
I wouldn't recommend keeping the file open all the time (when would you
close it?). The reason Stephany's code doesn't need an explicit
constructor is because the only initialization it does can be specified
as a variable initialization. If you had, say, a variable you wanted
initialized to the day of the month (ie a non-constant) you would do
something like

Private Shared dayOfMonth As Integer

Public Shared Sub New()
' this is the shared constructor
dayOfMonth = Today.Day
End Sub

2. If the object in Stephany's class, m_lock is used to lock the sub so
only one thread uses it at a time can be incrementing or decrementing
the count. Just for curiosity sake can my new class also have a object
called m_lock that would work separately from the one in mythreadcount?
Yes
I know I can name it something else. I'm trying to understand where
m_lock is visible. Can 2 classes have objects named m_lock and they not
interfere with each other?


Yes. If they are Public then they would be visible outside their class,
which wouldn't be appropriate, so make them Private.

--
Larry Lard
Replies to group please

Feb 23 '06 #11
cj,

A class with only shared members is in my opinion in fact not a class it is
a module.

It is direct instanced (and that is the only reason people can call it a
class) in your main program thread and will never leave it, in the same way
as with a module. The advance of an Class above a module in VBNet is that
you can describe it as a class and call the members as in a class by
instance Cj.Mymethode, with what it is more descripting.

For me a real class creates (instances) an object on the managed heap. If
that object is not needed anymore than it is released (the memory is given
back) automaticly by the garbage collector. What is one of the advantages of
managed code.

I hope that this gives an idea.

Cor
Feb 23 '06 #12
cj
Thanks, that does help.

Cor Ligthert [MVP] wrote:
cj,

A class with only shared members is in my opinion in fact not a class it is
a module.

It is direct instanced (and that is the only reason people can call it a
class) in your main program thread and will never leave it, in the same way
as with a module. The advance of an Class above a module in VBNet is that
you can describe it as a class and call the members as in a class by
instance Cj.Mymethode, with what it is more descripting.

For me a real class creates (instances) an object on the managed heap. If
that object is not needed anymore than it is released (the memory is given
back) automaticly by the garbage collector. What is one of the advantages of
managed code.

I hope that this gives an idea.

Cor

Feb 23 '06 #13
cj
Yes, I'm logging. the logging in the framework seems like another class
of worms that I don't have time to deal with if I opened them.

For performance sake I think the file does need to stay open--I can't
afford the time for it to open, write and close each time. It'll close
when the program closes. I've got multiple threads running so all of
them could conceivably be trying to update the file at the same time. I
don't know what the framework logging does about this but I know using
the basic structure of Stephany's code I can write the line myself while
ensuring threads don't step on each other. I'm just not sure if the
file is opened by the program when it starts if the threads that break
off from the program will be able to see it.
Larry Lard wrote:
cj wrote:
Maybe I can make this a bit clearer. I'm trying now to write a class
very much like this except it will have 1 sub which writes a line to a
txt file. The classes sub writeline will be called from withing many
concurrently running threads.


If you're doing some kind of logging then there are mechanisms in the
Framework that will handle this better than any user code. See
<http://msdn.microsoft.com/library/en-us/vbcon/html/vbconIntroductionToInstrumentationTracing.asp>
= <http://tinyurl.com/zprec> for more.
Questions in my mind:
1. Where do I open the file--I want it to stay open not be opened just
prior to each write. I think I'd want to do that in a constructor? Is
this right? Stephany's program doesn't seem to have a constructor. Yet
somehow it initializes m_threadcount to 0 only once.


I wouldn't recommend keeping the file open all the time (when would you
close it?). The reason Stephany's code doesn't need an explicit
constructor is because the only initialization it does can be specified
as a variable initialization. If you had, say, a variable you wanted
initialized to the day of the month (ie a non-constant) you would do
something like

Private Shared dayOfMonth As Integer

Public Shared Sub New()
' this is the shared constructor
dayOfMonth = Today.Day
End Sub
2. If the object in Stephany's class, m_lock is used to lock the sub so
only one thread uses it at a time can be incrementing or decrementing
the count. Just for curiosity sake can my new class also have a object
called m_lock that would work separately from the one in mythreadcount?


Yes
I know I can name it something else. I'm trying to understand where
m_lock is visible. Can 2 classes have objects named m_lock and they not
interfere with each other?


Yes. If they are Public then they would be visible outside their class,
which wouldn't be appropriate, so make them Private.

Feb 23 '06 #14

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

Similar topics

8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
15
by: Alex McMillan | last post by:
Hey - I know a bit about vb, written a few progs, upgraded to .net 2003 but can't figure it out. Where has everything gone? Am I missing something really obvious? I'm trying to create a...
8
by: Peter Newman | last post by:
Im running vb.net 2003. Im still on a big learning curve with .net and like most people have great ideas on what id like a peice of software to do.... but lack the knowledge to do it I have...
1
by: g35rider | last post by:
Hi, I am trying to understand from C++ point of view what the problem is. I have a file called. Controllers.h in which I have a base class Controller and couple of other sub classes...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
20
by: walterbyrd | last post by:
Reading "Think Like a Computer Scientist" I am not sure I understand the way it describes the way objects work with Python. 1) Can attributes can added just anywhere? I create an object called...
5
by: QbProg | last post by:
Hello, I did some experiments with VC++ 2005, and some ILDasm. Please tell me if I have understood the concepts of C++ programming under .NET, since I'm a bit confused! -- There are 4 types of...
3
by: sam | last post by:
I'm trying to better understand the relationship between unmanaged dlls and accessing them from a managed language such as vb.net.... 1-As I understand it, a DLL created in C++ as a win32 dll can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.