What exactly does it mean when they say

Discussion in 'Microsoft VB .NET' started by cj, Jul 5, 2006.

  1. cj

    cj Guest

    members of this type are safe for multithreaded operations. Instance
    members are not guaranteed to be thread-safe.


    I'm under the impression before you can use a class you have to make an
    instance of it. So how can a class be threadsafe by itself but an
    instance of it not be?

    I guess I don't get what exactly being threadsafe means. Multiple
    theads can use the same instance of a class?
     
    cj, Jul 5, 2006
    #1
    1. Advertisements

  2. Those classes may have shared methods. In which case, those methods are
    thread safe. So if multiple threads are calling the same shared method,
    that is safe and no extra precautions need to be taken by the developer.

    If 2 threads are pointing to the same instance of the class, then those
    instance methods are not guaranteed to be thread safe, and it is up to the
    developer to synchronize access.

    "cj" <> wrote in message
    news:...
    > members of this type are safe for multithreaded operations. Instance
    > members are not guaranteed to be thread-safe.
    >
    >
    > I'm under the impression before you can use a class you have to make an
    > instance of it. So how can a class be threadsafe by itself but an
    > instance of it not be?
    >
    > I guess I don't get what exactly being threadsafe means. Multiple theads
    > can use the same instance of a class?
     
    Marina Levit [MVP], Jul 5, 2006
    #2
    1. Advertisements

  3. cj

    cj Guest

    Sorry but to me these two situations sound the same. I don't understand
    what the difference is. To use a class I have to create an instance of
    it, right? Then I have multiple threads that will want to call methods
    in that instance of the class. Not thread safe? Or thread safe?

    Marina Levit [MVP] wrote:
    > Those classes may have shared methods. In which case, those methods are
    > thread safe. So if multiple threads are calling the same shared method,
    > that is safe and no extra precautions need to be taken by the developer.
    >
    > If 2 threads are pointing to the same instance of the class, then those
    > instance methods are not guaranteed to be thread safe, and it is up to the
    > developer to synchronize access.
    >
    > "cj" <> wrote in message
    > news:...
    >> members of this type are safe for multithreaded operations. Instance
    >> members are not guaranteed to be thread-safe.
    >>
    >>
    >> I'm under the impression before you can use a class you have to make an
    >> instance of it. So how can a class be threadsafe by itself but an
    >> instance of it not be?
    >>
    >> I guess I don't get what exactly being threadsafe means. Multiple theads
    >> can use the same instance of a class?

    >
    >
     
    cj, Jul 5, 2006
    #3
  4. Hello cj,

    > Sorry but to me these two situations sound the same. I don't
    > understand what the difference is. To use a class I have to create an
    > instance of it, right? Then I have multiple threads that will want to
    > call methods in that instance of the class. Not thread safe? Or
    > thread safe?


    That case is not thread safe. The case that is thread safe are when you
    call the "shared" methods. These are not associated with an instance and
    the documentation indicates they are safe for multi-threaded access.

    --
    Jared Parsons [MSFT]

    All opinions are my own. All content is provided "AS IS" with no warranties,
    and confers no rights.
     
    Jared Parsons [MSFT], Jul 5, 2006
    #4
  5. cj wrote:
    > Sorry but to me these two situations sound the same. I don't understand
    > what the difference is. To use a class I have to create an instance of
    > it, right?


    No.

    There are two types of methods on classes. Instance methods and Shared
    methods.

    An instance method is declared:
    Public Sub Foo()
    ....
    End Sub

    A shared method is declared:
    Public Shared Sub Bar()
    ....
    End Sub

    If the class is called Foobar, then to call Foo:

    dim o as new Foobar
    o.Foo()

    Notice I had to create an instance first. But to call Bar:

    Foobar.Bar()

    I don't have to create an instance because it is _shared_. I simply
    use the name of the class. In C++-speak, it's a static method. In OOP
    speak, a _class_ method.

    Example in the framework:
    Int32.Parse("42")
     
    Travers Naran, Jul 5, 2006
    #5
  6. No, you do not always need an instance of a class to use it.

    I think you need to read up on what shared methods are.

    "cj" <> wrote in message
    news:eP%...
    > Sorry but to me these two situations sound the same. I don't understand
    > what the difference is. To use a class I have to create an instance of
    > it, right? Then I have multiple threads that will want to call methods in
    > that instance of the class. Not thread safe? Or thread safe?
    >
    > Marina Levit [MVP] wrote:
    >> Those classes may have shared methods. In which case, those methods are
    >> thread safe. So if multiple threads are calling the same shared method,
    >> that is safe and no extra precautions need to be taken by the developer.
    >>
    >> If 2 threads are pointing to the same instance of the class, then those
    >> instance methods are not guaranteed to be thread safe, and it is up to
    >> the developer to synchronize access.
    >>
    >> "cj" <> wrote in message
    >> news:...
    >>> members of this type are safe for multithreaded operations. Instance
    >>> members are not guaranteed to be thread-safe.
    >>>
    >>>
    >>> I'm under the impression before you can use a class you have to make an
    >>> instance of it. So how can a class be threadsafe by itself but an
    >>> instance of it not be?
    >>>
    >>> I guess I don't get what exactly being threadsafe means. Multiple
    >>> theads can use the same instance of a class?

    >>
     
    Marina Levit [MVP], Jul 5, 2006
    #6
  7. cj

    cj Guest

    Ok.

    But the act of declaring Public Shared alone doesn't make it threadsafe
    does it? I have this class MyThreadCount (see below) which creates
    public shared classes but I was told I had to use the synclock to keep
    multiple threads from manipulating the count at the same time. Is this
    true?

    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


    Travers Naran wrote:
    > cj wrote:
    >> Sorry but to me these two situations sound the same. I don't understand
    >> what the difference is. To use a class I have to create an instance of
    >> it, right?

    >
    > No.
    >
    > There are two types of methods on classes. Instance methods and Shared
    > methods.
    >
    > An instance method is declared:
    > Public Sub Foo()
    > ...
    > End Sub
    >
    > A shared method is declared:
    > Public Shared Sub Bar()
    > ...
    > End Sub
    >
    > If the class is called Foobar, then to call Foo:
    >
    > dim o as new Foobar
    > o.Foo()
    >
    > Notice I had to create an instance first. But to call Bar:
    >
    > Foobar.Bar()
    >
    > I don't have to create an instance because it is _shared_. I simply
    > use the name of the class. In C++-speak, it's a static method. In OOP
    > speak, a _class_ method.
    >
    > Example in the framework:
    > Int32.Parse("42")
    >
     
    cj, Jul 5, 2006
    #7
  8. No, having a method declared as Shared does not automatically make it thread
    safe.

    But if there is a note in the documentation that says it is thread safe,
    then developers of that method took precautions to make sure it is.

    "cj" <> wrote in message
    news:...
    > Ok.
    >
    > But the act of declaring Public Shared alone doesn't make it threadsafe
    > does it? I have this class MyThreadCount (see below) which creates public
    > shared classes but I was told I had to use the synclock to keep multiple
    > threads from manipulating the count at the same time. Is this true?
    >
    > 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
    >
    >
    > Travers Naran wrote:
    >> cj wrote:
    >>> Sorry but to me these two situations sound the same. I don't understand
    >>> what the difference is. To use a class I have to create an instance of
    >>> it, right?

    >>
    >> No.
    >>
    >> There are two types of methods on classes. Instance methods and Shared
    >> methods.
    >>
    >> An instance method is declared:
    >> Public Sub Foo()
    >> ...
    >> End Sub
    >>
    >> A shared method is declared:
    >> Public Shared Sub Bar()
    >> ...
    >> End Sub
    >>
    >> If the class is called Foobar, then to call Foo:
    >>
    >> dim o as new Foobar
    >> o.Foo()
    >>
    >> Notice I had to create an instance first. But to call Bar:
    >>
    >> Foobar.Bar()
    >>
    >> I don't have to create an instance because it is _shared_. I simply
    >> use the name of the class. In C++-speak, it's a static method. In OOP
    >> speak, a _class_ method.
    >>
    >> Example in the framework:
    >> Int32.Parse("42")
    >>
     
    Marina Levit [MVP], Jul 5, 2006
    #8
  9. cj

    cj Guest

    Ok, believe it or not were making progress. I think between my
    threadcount class and Travers examples I'm getting an idea of how to
    write a class that's threadsafe or not.

    But for MS classes... Right now I'm looking at the Queue class and it
    says, "Public static (Shared in Visual Basic) members of this type are
    safe for multithreaded operations." How do I tell which of the methods
    listed under Queue Members are public static? They list Public
    Constructor, Public Properties, Public Methods, and protected Methods.
    I see the thread safe message a lot and I would like to know what
    exactly it's saying is and isn't threadsafe about the class or how I use
    it. If there is a better MS class example than the Queue class feel
    free to point it out.


    Marina Levit [MVP] wrote:
    > No, having a method declared as Shared does not automatically make it thread
    > safe.
    >
    > But if there is a note in the documentation that says it is thread safe,
    > then developers of that method took precautions to make sure it is.
    >
    > "cj" <> wrote in message
    > news:...
    >> Ok.
    >>
    >> But the act of declaring Public Shared alone doesn't make it threadsafe
    >> does it? I have this class MyThreadCount (see below) which creates public
    >> shared classes but I was told I had to use the synclock to keep multiple
    >> threads from manipulating the count at the same time. Is this true?
    >>
    >> 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
    >>
    >>
    >> Travers Naran wrote:
    >>> cj wrote:
    >>>> Sorry but to me these two situations sound the same. I don't understand
    >>>> what the difference is. To use a class I have to create an instance of
    >>>> it, right?
    >>> No.
    >>>
    >>> There are two types of methods on classes. Instance methods and Shared
    >>> methods.
    >>>
    >>> An instance method is declared:
    >>> Public Sub Foo()
    >>> ...
    >>> End Sub
    >>>
    >>> A shared method is declared:
    >>> Public Shared Sub Bar()
    >>> ...
    >>> End Sub
    >>>
    >>> If the class is called Foobar, then to call Foo:
    >>>
    >>> dim o as new Foobar
    >>> o.Foo()
    >>>
    >>> Notice I had to create an instance first. But to call Bar:
    >>>
    >>> Foobar.Bar()
    >>>
    >>> I don't have to create an instance because it is _shared_. I simply
    >>> use the name of the class. In C++-speak, it's a static method. In OOP
    >>> speak, a _class_ method.
    >>>
    >>> Example in the framework:
    >>> Int32.Parse("42")
    >>>

    >
    >
     
    cj, Jul 5, 2006
    #9
  10. There is usually an icon in the documentation to indicate that a method is
    shared. When you look at the help for that method, it says it is shared
    there as well.

    A lot of these classes don't actually have any useful shared methods that
    you would ever call. You would always end up creating an instance of the
    class, at which point those methods on that object are not thread safe.

    Some classes might actually have shared methods that you would want to call.
    It all depends on the class.

    "cj" <> wrote in message
    news:%...
    > Ok, believe it or not were making progress. I think between my
    > threadcount class and Travers examples I'm getting an idea of how to write
    > a class that's threadsafe or not.
    >
    > But for MS classes... Right now I'm looking at the Queue class and it
    > says, "Public static (Shared in Visual Basic) members of this type are
    > safe for multithreaded operations." How do I tell which of the methods
    > listed under Queue Members are public static? They list Public
    > Constructor, Public Properties, Public Methods, and protected Methods. I
    > see the thread safe message a lot and I would like to know what exactly
    > it's saying is and isn't threadsafe about the class or how I use it. If
    > there is a better MS class example than the Queue class feel free to point
    > it out.
    >
    >
    > Marina Levit [MVP] wrote:
    >> No, having a method declared as Shared does not automatically make it
    >> thread safe.
    >>
    >> But if there is a note in the documentation that says it is thread safe,
    >> then developers of that method took precautions to make sure it is.
    >>
    >> "cj" <> wrote in message
    >> news:...
    >>> Ok.
    >>>
    >>> But the act of declaring Public Shared alone doesn't make it threadsafe
    >>> does it? I have this class MyThreadCount (see below) which creates
    >>> public shared classes but I was told I had to use the synclock to keep
    >>> multiple threads from manipulating the count at the same time. Is this
    >>> true?
    >>>
    >>> 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
    >>>
    >>>
    >>> Travers Naran wrote:
    >>>> cj wrote:
    >>>>> Sorry but to me these two situations sound the same. I don't
    >>>>> understand
    >>>>> what the difference is. To use a class I have to create an instance
    >>>>> of
    >>>>> it, right?
    >>>> No.
    >>>>
    >>>> There are two types of methods on classes. Instance methods and Shared
    >>>> methods.
    >>>>
    >>>> An instance method is declared:
    >>>> Public Sub Foo()
    >>>> ...
    >>>> End Sub
    >>>>
    >>>> A shared method is declared:
    >>>> Public Shared Sub Bar()
    >>>> ...
    >>>> End Sub
    >>>>
    >>>> If the class is called Foobar, then to call Foo:
    >>>>
    >>>> dim o as new Foobar
    >>>> o.Foo()
    >>>>
    >>>> Notice I had to create an instance first. But to call Bar:
    >>>>
    >>>> Foobar.Bar()
    >>>>
    >>>> I don't have to create an instance because it is _shared_. I simply
    >>>> use the name of the class. In C++-speak, it's a static method. In OOP
    >>>> speak, a _class_ method.
    >>>>
    >>>> Example in the framework:
    >>>> Int32.Parse("42")
    >>>>

    >>
     
    Marina Levit [MVP], Jul 5, 2006
    #10
  11. cj

    cj Guest

    Ok, I see now the S icon which says the synchronized method is the only
    method in the queue class that is thread safe and that explains that
    line about the synchronized wrapper. So two things are running through
    my head now. 1. I guess my using synclock is a good way of making my
    threadcount class threadsafe so many threads can use it w/o interfering
    with each other. Must be as it works. 2. I'm curious about using a
    shared method. I keep reading that you have to instigate a class for it
    to become an object and use it. Clearly you don't but that confuses me
    as to how to view it. Is it an object w/o instigation?

    Marina Levit [MVP] wrote:
    > There is usually an icon in the documentation to indicate that a method is
    > shared. When you look at the help for that method, it says it is shared
    > there as well.
    >
    > A lot of these classes don't actually have any useful shared methods that
    > you would ever call. You would always end up creating an instance of the
    > class, at which point those methods on that object are not thread safe.
    >
    > Some classes might actually have shared methods that you would want to call.
    > It all depends on the class.
    >
    > "cj" <> wrote in message
    > news:%...
    >> Ok, believe it or not were making progress. I think between my
    >> threadcount class and Travers examples I'm getting an idea of how to write
    >> a class that's threadsafe or not.
    >>
    >> But for MS classes... Right now I'm looking at the Queue class and it
    >> says, "Public static (Shared in Visual Basic) members of this type are
    >> safe for multithreaded operations." How do I tell which of the methods
    >> listed under Queue Members are public static? They list Public
    >> Constructor, Public Properties, Public Methods, and protected Methods. I
    >> see the thread safe message a lot and I would like to know what exactly
    >> it's saying is and isn't threadsafe about the class or how I use it. If
    >> there is a better MS class example than the Queue class feel free to point
    >> it out.
    >>
    >>
    >> Marina Levit [MVP] wrote:
    >>> No, having a method declared as Shared does not automatically make it
    >>> thread safe.
    >>>
    >>> But if there is a note in the documentation that says it is thread safe,
    >>> then developers of that method took precautions to make sure it is.
    >>>
    >>> "cj" <> wrote in message
    >>> news:...
    >>>> Ok.
    >>>>
    >>>> But the act of declaring Public Shared alone doesn't make it threadsafe
    >>>> does it? I have this class MyThreadCount (see below) which creates
    >>>> public shared classes but I was told I had to use the synclock to keep
    >>>> multiple threads from manipulating the count at the same time. Is this
    >>>> true?
    >>>>
    >>>> 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
    >>>>
    >>>>
    >>>> Travers Naran wrote:
    >>>>> cj wrote:
    >>>>>> Sorry but to me these two situations sound the same. I don't
    >>>>>> understand
    >>>>>> what the difference is. To use a class I have to create an instance
    >>>>>> of
    >>>>>> it, right?
    >>>>> No.
    >>>>>
    >>>>> There are two types of methods on classes. Instance methods and Shared
    >>>>> methods.
    >>>>>
    >>>>> An instance method is declared:
    >>>>> Public Sub Foo()
    >>>>> ...
    >>>>> End Sub
    >>>>>
    >>>>> A shared method is declared:
    >>>>> Public Shared Sub Bar()
    >>>>> ...
    >>>>> End Sub
    >>>>>
    >>>>> If the class is called Foobar, then to call Foo:
    >>>>>
    >>>>> dim o as new Foobar
    >>>>> o.Foo()
    >>>>>
    >>>>> Notice I had to create an instance first. But to call Bar:
    >>>>>
    >>>>> Foobar.Bar()
    >>>>>
    >>>>> I don't have to create an instance because it is _shared_. I simply
    >>>>> use the name of the class. In C++-speak, it's a static method. In OOP
    >>>>> speak, a _class_ method.
    >>>>>
    >>>>> Example in the framework:
    >>>>> Int32.Parse("42")
    >>>>>

    >
     
    cj, Jul 5, 2006
    #11
  12. Sorry, I don't know what 'instigation' is. Let's say you have a class named
    MyClass with a shared method called MySharedMethod.

    In your code, you can just call MyClass.MySharedMethod(). That is all. You
    don't need to do anything else.

    There is no object created here - there is no instance of MyClass.
    MySharedMethod belongs to the type, and not to any particular instance of
    that type.

    "cj" <> wrote in message
    news:...
    > Ok, I see now the S icon which says the synchronized method is the only
    > method in the queue class that is thread safe and that explains that line
    > about the synchronized wrapper. So two things are running through my head
    > now. 1. I guess my using synclock is a good way of making my threadcount
    > class threadsafe so many threads can use it w/o interfering with each
    > other. Must be as it works. 2. I'm curious about using a shared method.
    > I keep reading that you have to instigate a class for it to become an
    > object and use it. Clearly you don't but that confuses me as to how to
    > view it. Is it an object w/o instigation?
    >
    > Marina Levit [MVP] wrote:
    >> There is usually an icon in the documentation to indicate that a method
    >> is shared. When you look at the help for that method, it says it is
    >> shared there as well.
    >>
    >> A lot of these classes don't actually have any useful shared methods that
    >> you would ever call. You would always end up creating an instance of the
    >> class, at which point those methods on that object are not thread safe.
    >>
    >> Some classes might actually have shared methods that you would want to
    >> call. It all depends on the class.
    >>
    >> "cj" <> wrote in message
    >> news:%...
    >>> Ok, believe it or not were making progress. I think between my
    >>> threadcount class and Travers examples I'm getting an idea of how to
    >>> write a class that's threadsafe or not.
    >>>
    >>> But for MS classes... Right now I'm looking at the Queue class and it
    >>> says, "Public static (Shared in Visual Basic) members of this type are
    >>> safe for multithreaded operations." How do I tell which of the methods
    >>> listed under Queue Members are public static? They list Public
    >>> Constructor, Public Properties, Public Methods, and protected Methods. I
    >>> see the thread safe message a lot and I would like to know what exactly
    >>> it's saying is and isn't threadsafe about the class or how I use it. If
    >>> there is a better MS class example than the Queue class feel free to
    >>> point it out.
    >>>
    >>>
    >>> Marina Levit [MVP] wrote:
    >>>> No, having a method declared as Shared does not automatically make it
    >>>> thread safe.
    >>>>
    >>>> But if there is a note in the documentation that says it is thread
    >>>> safe, then developers of that method took precautions to make sure it
    >>>> is.
    >>>>
    >>>> "cj" <> wrote in message
    >>>> news:...
    >>>>> Ok.
    >>>>>
    >>>>> But the act of declaring Public Shared alone doesn't make it
    >>>>> threadsafe does it? I have this class MyThreadCount (see below) which
    >>>>> creates public shared classes but I was told I had to use the synclock
    >>>>> to keep multiple threads from manipulating the count at the same time.
    >>>>> Is this true?
    >>>>>
    >>>>> 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
    >>>>>
    >>>>>
    >>>>> Travers Naran wrote:
    >>>>>> cj wrote:
    >>>>>>> Sorry but to me these two situations sound the same. I don't
    >>>>>>> understand
    >>>>>>> what the difference is. To use a class I have to create an instance
    >>>>>>> of
    >>>>>>> it, right?
    >>>>>> No.
    >>>>>>
    >>>>>> There are two types of methods on classes. Instance methods and
    >>>>>> Shared
    >>>>>> methods.
    >>>>>>
    >>>>>> An instance method is declared:
    >>>>>> Public Sub Foo()
    >>>>>> ...
    >>>>>> End Sub
    >>>>>>
    >>>>>> A shared method is declared:
    >>>>>> Public Shared Sub Bar()
    >>>>>> ...
    >>>>>> End Sub
    >>>>>>
    >>>>>> If the class is called Foobar, then to call Foo:
    >>>>>>
    >>>>>> dim o as new Foobar
    >>>>>> o.Foo()
    >>>>>>
    >>>>>> Notice I had to create an instance first. But to call Bar:
    >>>>>>
    >>>>>> Foobar.Bar()
    >>>>>>
    >>>>>> I don't have to create an instance because it is _shared_. I simply
    >>>>>> use the name of the class. In C++-speak, it's a static method. In
    >>>>>> OOP
    >>>>>> speak, a _class_ method.
    >>>>>>
    >>>>>> Example in the framework:
    >>>>>> Int32.Parse("42")
    >>>>>>

    >>
     
    Marina Levit [MVP], Jul 5, 2006
    #12
  13. cj

    cj Guest

    My fault it's Instantiation not Instigation which frankly now that I've
    looked up both in the dictionary Instantiation is to create while
    Instigation means to start. Still seem quite the same. No wonder I
    always thought it was Instigation.


    Marina Levit [MVP] wrote:
    > Sorry, I don't know what 'instigation' is. Let's say you have a class named
    > MyClass with a shared method called MySharedMethod.
    >
    > In your code, you can just call MyClass.MySharedMethod(). That is all. You
    > don't need to do anything else.
    >
    > There is no object created here - there is no instance of MyClass.
    > MySharedMethod belongs to the type, and not to any particular instance of
    > that type.
    >
    > "cj" <> wrote in message
    > news:...
    >> Ok, I see now the S icon which says the synchronized method is the only
    >> method in the queue class that is thread safe and that explains that line
    >> about the synchronized wrapper. So two things are running through my head
    >> now. 1. I guess my using synclock is a good way of making my threadcount
    >> class threadsafe so many threads can use it w/o interfering with each
    >> other. Must be as it works. 2. I'm curious about using a shared method.
    >> I keep reading that you have to instigate a class for it to become an
    >> object and use it. Clearly you don't but that confuses me as to how to
    >> view it. Is it an object w/o instigation?
    >>
    >> Marina Levit [MVP] wrote:
    >>> There is usually an icon in the documentation to indicate that a method
    >>> is shared. When you look at the help for that method, it says it is
    >>> shared there as well.
    >>>
    >>> A lot of these classes don't actually have any useful shared methods that
    >>> you would ever call. You would always end up creating an instance of the
    >>> class, at which point those methods on that object are not thread safe.
    >>>
    >>> Some classes might actually have shared methods that you would want to
    >>> call. It all depends on the class.
    >>>
    >>> "cj" <> wrote in message
    >>> news:%...
    >>>> Ok, believe it or not were making progress. I think between my
    >>>> threadcount class and Travers examples I'm getting an idea of how to
    >>>> write a class that's threadsafe or not.
    >>>>
    >>>> But for MS classes... Right now I'm looking at the Queue class and it
    >>>> says, "Public static (Shared in Visual Basic) members of this type are
    >>>> safe for multithreaded operations." How do I tell which of the methods
    >>>> listed under Queue Members are public static? They list Public
    >>>> Constructor, Public Properties, Public Methods, and protected Methods. I
    >>>> see the thread safe message a lot and I would like to know what exactly
    >>>> it's saying is and isn't threadsafe about the class or how I use it. If
    >>>> there is a better MS class example than the Queue class feel free to
    >>>> point it out.
    >>>>
    >>>>
    >>>> Marina Levit [MVP] wrote:
    >>>>> No, having a method declared as Shared does not automatically make it
    >>>>> thread safe.
    >>>>>
    >>>>> But if there is a note in the documentation that says it is thread
    >>>>> safe, then developers of that method took precautions to make sure it
    >>>>> is.
    >>>>>
    >>>>> "cj" <> wrote in message
    >>>>> news:...
    >>>>>> Ok.
    >>>>>>
    >>>>>> But the act of declaring Public Shared alone doesn't make it
    >>>>>> threadsafe does it? I have this class MyThreadCount (see below) which
    >>>>>> creates public shared classes but I was told I had to use the synclock
    >>>>>> to keep multiple threads from manipulating the count at the same time.
    >>>>>> Is this true?
    >>>>>>
    >>>>>> 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
    >>>>>>
    >>>>>>
    >>>>>> Travers Naran wrote:
    >>>>>>> cj wrote:
    >>>>>>>> Sorry but to me these two situations sound the same. I don't
    >>>>>>>> understand
    >>>>>>>> what the difference is. To use a class I have to create an instance
    >>>>>>>> of
    >>>>>>>> it, right?
    >>>>>>> No.
    >>>>>>>
    >>>>>>> There are two types of methods on classes. Instance methods and
    >>>>>>> Shared
    >>>>>>> methods.
    >>>>>>>
    >>>>>>> An instance method is declared:
    >>>>>>> Public Sub Foo()
    >>>>>>> ...
    >>>>>>> End Sub
    >>>>>>>
    >>>>>>> A shared method is declared:
    >>>>>>> Public Shared Sub Bar()
    >>>>>>> ...
    >>>>>>> End Sub
    >>>>>>>
    >>>>>>> If the class is called Foobar, then to call Foo:
    >>>>>>>
    >>>>>>> dim o as new Foobar
    >>>>>>> o.Foo()
    >>>>>>>
    >>>>>>> Notice I had to create an instance first. But to call Bar:
    >>>>>>>
    >>>>>>> Foobar.Bar()
    >>>>>>>
    >>>>>>> I don't have to create an instance because it is _shared_. I simply
    >>>>>>> use the name of the class. In C++-speak, it's a static method. In
    >>>>>>> OOP
    >>>>>>> speak, a _class_ method.
    >>>>>>>
    >>>>>>> Example in the framework:
    >>>>>>> Int32.Parse("42")
    >>>>>>>

    >
    >
     
    cj, Jul 5, 2006
    #13
  14. Hi Cj,

    Do you understand the difference between Shared methods and normal object
    methods? This is a key concept in OOP area. Normally, Shared methods
    belongs to the class itself, while normal object methods belongs to certain
    instance of the class. So invoking Shared methods needs only the class
    name, not initializing an object instance, while invoking an object method,
    you have to create an instance of the class first and use this object
    instance reference to invoke the method.

    From "Queue Class" in MSDN, we can read that:
    "Public static (Shared in Visual Basic) members of this type are safe for
    multithreaded operations. Instance members are not guaranteed to be
    thread-safe."

    So Queue.Synchronized(this is the only shared/static member of Queue class)
    method can be invoked by multithreads without any problem, while other
    members of "Queue Class" should be protected with synclock.

    Hope this is clear.

    Best regards,
    Jeffrey Tan
    Microsoft Online Community Support
    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Jeffrey Tan[MSFT], Jul 6, 2006
    #14
  15. cj,

    Your application has this patron in memory when you are running

    Main module part 'created by the mainform or by yourself
    Modules or Shared classes what both is the same

    Somewhere temporary in memory as long as needed managed by your managed code
    Instanced objects

    Very confusing is that some people call often an object a Class and typical
    C# people call a module as well a class (static class although there is in
    my idea not much static anymore about it).

    A class is nothing more than a template from a certain type that can be
    instanced.

    When I did read yesterday the message from Marina, I had the idea that she
    had made a kind of typo telling that a shared class did belong to a type,
    while it is to the main (stack) modules or in C# language main static class.

    I had however the idea that you did understand it, therefore I did not place
    any message.

    I hope this makes it a little bit clearer.

    Cor


    "cj" <> schreef in bericht
    news:%23T$...
    > My fault it's Instantiation not Instigation which frankly now that I've
    > looked up both in the dictionary Instantiation is to create while
    > Instigation means to start. Still seem quite the same. No wonder I
    > always thought it was Instigation.
    >
    >
    > Marina Levit [MVP] wrote:
    >> Sorry, I don't know what 'instigation' is. Let's say you have a class
    >> named MyClass with a shared method called MySharedMethod.
    >>
    >> In your code, you can just call MyClass.MySharedMethod(). That is all.
    >> You don't need to do anything else.
    >>
    >> There is no object created here - there is no instance of MyClass.
    >> MySharedMethod belongs to the type, and not to any particular instance of
    >> that type.
    >>
    >> "cj" <> wrote in message
    >> news:...
    >>> Ok, I see now the S icon which says the synchronized method is the only
    >>> method in the queue class that is thread safe and that explains that
    >>> line about the synchronized wrapper. So two things are running through
    >>> my head now. 1. I guess my using synclock is a good way of making my
    >>> threadcount class threadsafe so many threads can use it w/o interfering
    >>> with each other. Must be as it works. 2. I'm curious about using a
    >>> shared method. I keep reading that you have to instigate a class for it
    >>> to become an object and use it. Clearly you don't but that confuses me
    >>> as to how to view it. Is it an object w/o instigation?
    >>>
    >>> Marina Levit [MVP] wrote:
    >>>> There is usually an icon in the documentation to indicate that a method
    >>>> is shared. When you look at the help for that method, it says it is
    >>>> shared there as well.
    >>>>
    >>>> A lot of these classes don't actually have any useful shared methods
    >>>> that you would ever call. You would always end up creating an instance
    >>>> of the class, at which point those methods on that object are not
    >>>> thread safe.
    >>>>
    >>>> Some classes might actually have shared methods that you would want to
    >>>> call. It all depends on the class.
    >>>>
    >>>> "cj" <> wrote in message
    >>>> news:%...
    >>>>> Ok, believe it or not were making progress. I think between my
    >>>>> threadcount class and Travers examples I'm getting an idea of how to
    >>>>> write a class that's threadsafe or not.
    >>>>>
    >>>>> But for MS classes... Right now I'm looking at the Queue class and it
    >>>>> says, "Public static (Shared in Visual Basic) members of this type are
    >>>>> safe for multithreaded operations." How do I tell which of the
    >>>>> methods listed under Queue Members are public static? They list
    >>>>> Public Constructor, Public Properties, Public Methods, and protected
    >>>>> Methods. I see the thread safe message a lot and I would like to know
    >>>>> what exactly it's saying is and isn't threadsafe about the class or
    >>>>> how I use it. If there is a better MS class example than the Queue
    >>>>> class feel free to point it out.
    >>>>>
    >>>>>
    >>>>> Marina Levit [MVP] wrote:
    >>>>>> No, having a method declared as Shared does not automatically make it
    >>>>>> thread safe.
    >>>>>>
    >>>>>> But if there is a note in the documentation that says it is thread
    >>>>>> safe, then developers of that method took precautions to make sure it
    >>>>>> is.
    >>>>>>
    >>>>>> "cj" <> wrote in message
    >>>>>> news:...
    >>>>>>> Ok.
    >>>>>>>
    >>>>>>> But the act of declaring Public Shared alone doesn't make it
    >>>>>>> threadsafe does it? I have this class MyThreadCount (see below)
    >>>>>>> which creates public shared classes but I was told I had to use the
    >>>>>>> synclock to keep multiple threads from manipulating the count at the
    >>>>>>> same time. Is this true?
    >>>>>>>
    >>>>>>> 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
    >>>>>>>
    >>>>>>>
    >>>>>>> Travers Naran wrote:
    >>>>>>>> cj wrote:
    >>>>>>>>> Sorry but to me these two situations sound the same. I don't
    >>>>>>>>> understand
    >>>>>>>>> what the difference is. To use a class I have to create an
    >>>>>>>>> instance of
    >>>>>>>>> it, right?
    >>>>>>>> No.
    >>>>>>>>
    >>>>>>>> There are two types of methods on classes. Instance methods and
    >>>>>>>> Shared
    >>>>>>>> methods.
    >>>>>>>>
    >>>>>>>> An instance method is declared:
    >>>>>>>> Public Sub Foo()
    >>>>>>>> ...
    >>>>>>>> End Sub
    >>>>>>>>
    >>>>>>>> A shared method is declared:
    >>>>>>>> Public Shared Sub Bar()
    >>>>>>>> ...
    >>>>>>>> End Sub
    >>>>>>>>
    >>>>>>>> If the class is called Foobar, then to call Foo:
    >>>>>>>>
    >>>>>>>> dim o as new Foobar
    >>>>>>>> o.Foo()
    >>>>>>>>
    >>>>>>>> Notice I had to create an instance first. But to call Bar:
    >>>>>>>>
    >>>>>>>> Foobar.Bar()
    >>>>>>>>
    >>>>>>>> I don't have to create an instance because it is _shared_. I
    >>>>>>>> simply
    >>>>>>>> use the name of the class. In C++-speak, it's a static method. In
    >>>>>>>> OOP
    >>>>>>>> speak, a _class_ method.
    >>>>>>>>
    >>>>>>>> Example in the framework:
    >>>>>>>> Int32.Parse("42")
    >>>>>>>>

    >>
     
    Cor Ligthert [MVP], Jul 6, 2006
    #15
  16. cj

    cj Guest

    Jeffrey and Cor, I keep hearing and even read in Cor's response "A
    class is nothing more than a template from a certain type that can be
    instanced." yet if it's a shared method in a class the class doesn't
    have to be instanced. I'm trying to think in the cookie cutter as a
    class and each cookie as an instance of it. Yet it appears the cookie
    cutter has shared methods and can be used without making a cookie. What
    would be an example of this?

    Then in this code I'm currently using I never make an instance of this
    class yet it functions. I know it's defined as Public Class. So why do
    I keep hearing a class is a model like a cookie cutter to make instances
    of (cookies) and then in practice it appears many times a class doesn't
    need an instance made of it at all?

    And what if a class had a shared method and instances are made of it?
    How could this work? Would all instances change when the shared method
    is used? Can you give me a cookie cutter example of that?

    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




    Cor Ligthert [MVP] wrote:
    > cj,
    >
    > Your application has this patron in memory when you are running
    >
    > Main module part 'created by the mainform or by yourself
    > Modules or Shared classes what both is the same
    >
    > Somewhere temporary in memory as long as needed managed by your managed code
    > Instanced objects
    >
    > Very confusing is that some people call often an object a Class and typical
    > C# people call a module as well a class (static class although there is in
    > my idea not much static anymore about it).
    >
    > A class is nothing more than a template from a certain type that can be
    > instanced.
    >
    > When I did read yesterday the message from Marina, I had the idea that she
    > had made a kind of typo telling that a shared class did belong to a type,
    > while it is to the main (stack) modules or in C# language main static class.
    >
    > I had however the idea that you did understand it, therefore I did not place
    > any message.
    >
    > I hope this makes it a little bit clearer.
    >
    > Cor
    >
    >
    > "cj" <> schreef in bericht
    > news:%23T$...
    >> My fault it's Instantiation not Instigation which frankly now that I've
    >> looked up both in the dictionary Instantiation is to create while
    >> Instigation means to start. Still seem quite the same. No wonder I
    >> always thought it was Instigation.
    >>
    >>
    >> Marina Levit [MVP] wrote:
    >>> Sorry, I don't know what 'instigation' is. Let's say you have a class
    >>> named MyClass with a shared method called MySharedMethod.
    >>>
    >>> In your code, you can just call MyClass.MySharedMethod(). That is all.
    >>> You don't need to do anything else.
    >>>
    >>> There is no object created here - there is no instance of MyClass.
    >>> MySharedMethod belongs to the type, and not to any particular instance of
    >>> that type.
    >>>
    >>> "cj" <> wrote in message
    >>> news:...
    >>>> Ok, I see now the S icon which says the synchronized method is the only
    >>>> method in the queue class that is thread safe and that explains that
    >>>> line about the synchronized wrapper. So two things are running through
    >>>> my head now. 1. I guess my using synclock is a good way of making my
    >>>> threadcount class threadsafe so many threads can use it w/o interfering
    >>>> with each other. Must be as it works. 2. I'm curious about using a
    >>>> shared method. I keep reading that you have to instigate a class for it
    >>>> to become an object and use it. Clearly you don't but that confuses me
    >>>> as to how to view it. Is it an object w/o instigation?
    >>>>
    >>>> Marina Levit [MVP] wrote:
    >>>>> There is usually an icon in the documentation to indicate that a method
    >>>>> is shared. When you look at the help for that method, it says it is
    >>>>> shared there as well.
    >>>>>
    >>>>> A lot of these classes don't actually have any useful shared methods
    >>>>> that you would ever call. You would always end up creating an instance
    >>>>> of the class, at which point those methods on that object are not
    >>>>> thread safe.
    >>>>>
    >>>>> Some classes might actually have shared methods that you would want to
    >>>>> call. It all depends on the class.
    >>>>>
    >>>>> "cj" <> wrote in message
    >>>>> news:%...
    >>>>>> Ok, believe it or not were making progress. I think between my
    >>>>>> threadcount class and Travers examples I'm getting an idea of how to
    >>>>>> write a class that's threadsafe or not.
    >>>>>>
    >>>>>> But for MS classes... Right now I'm looking at the Queue class and it
    >>>>>> says, "Public static (Shared in Visual Basic) members of this type are
    >>>>>> safe for multithreaded operations." How do I tell which of the
    >>>>>> methods listed under Queue Members are public static? They list
    >>>>>> Public Constructor, Public Properties, Public Methods, and protected
    >>>>>> Methods. I see the thread safe message a lot and I would like to know
    >>>>>> what exactly it's saying is and isn't threadsafe about the class or
    >>>>>> how I use it. If there is a better MS class example than the Queue
    >>>>>> class feel free to point it out.
    >>>>>>
    >>>>>>
    >>>>>> Marina Levit [MVP] wrote:
    >>>>>>> No, having a method declared as Shared does not automatically make it
    >>>>>>> thread safe.
    >>>>>>>
    >>>>>>> But if there is a note in the documentation that says it is thread
    >>>>>>> safe, then developers of that method took precautions to make sure it
    >>>>>>> is.
    >>>>>>>
    >>>>>>> "cj" <> wrote in message
    >>>>>>> news:...
    >>>>>>>> Ok.
    >>>>>>>>
    >>>>>>>> But the act of declaring Public Shared alone doesn't make it
    >>>>>>>> threadsafe does it? I have this class MyThreadCount (see below)
    >>>>>>>> which creates public shared classes but I was told I had to use the
    >>>>>>>> synclock to keep multiple threads from manipulating the count at the
    >>>>>>>> same time. Is this true?
    >>>>>>>>
    >>>>>>>> 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
    >>>>>>>>
    >>>>>>>>
    >>>>>>>> Travers Naran wrote:
    >>>>>>>>> cj wrote:
    >>>>>>>>>> Sorry but to me these two situations sound the same. I don't
    >>>>>>>>>> understand
    >>>>>>>>>> what the difference is. To use a class I have to create an
    >>>>>>>>>> instance of
    >>>>>>>>>> it, right?
    >>>>>>>>> No.
    >>>>>>>>>
    >>>>>>>>> There are two types of methods on classes. Instance methods and
    >>>>>>>>> Shared
    >>>>>>>>> methods.
    >>>>>>>>>
    >>>>>>>>> An instance method is declared:
    >>>>>>>>> Public Sub Foo()
    >>>>>>>>> ...
    >>>>>>>>> End Sub
    >>>>>>>>>
    >>>>>>>>> A shared method is declared:
    >>>>>>>>> Public Shared Sub Bar()
    >>>>>>>>> ...
    >>>>>>>>> End Sub
    >>>>>>>>>
    >>>>>>>>> If the class is called Foobar, then to call Foo:
    >>>>>>>>>
    >>>>>>>>> dim o as new Foobar
    >>>>>>>>> o.Foo()
    >>>>>>>>>
    >>>>>>>>> Notice I had to create an instance first. But to call Bar:
    >>>>>>>>>
    >>>>>>>>> Foobar.Bar()
    >>>>>>>>>
    >>>>>>>>> I don't have to create an instance because it is _shared_. I
    >>>>>>>>> simply
    >>>>>>>>> use the name of the class. In C++-speak, it's a static method. In
    >>>>>>>>> OOP
    >>>>>>>>> speak, a _class_ method.
    >>>>>>>>>
    >>>>>>>>> Example in the framework:
    >>>>>>>>> Int32.Parse("42")
    >>>>>>>>>

    >
     
    cj, Jul 6, 2006
    #16
  17. cj,

    I tried to keep it as short as possible however the names that are used and
    are just from history are very confusing.
    ..
    A Shared Class is nothing more as a module you could have written it as well
    in VB as this.

    Public Module MyThreadCount
    Private As New Object
    Private m_threadcount As Int32 = 0
    Public Sub Sub Increment()
    SyncLock (m_lock)
    m_threadcount += 1
    End SyncLock
    End Sub

    etc.

    A shared class is called in past a static class. In mine opinion is the name
    completely false.

    It is a class that is direct used as a module, it has no constructor (Sub
    New).

    I hope this gives an idea?

    Cor
     
    Cor Ligthert [MVP], Jul 6, 2006
    #17
  18. cj

    cj Guest

    Yes, that helps. Just another case of confusing names and terms.

    I'm still wondering can a class have shared methods and have instances
    of it?


    Cor Ligthert [MVP] wrote:
    > cj,
    >
    > I tried to keep it as short as possible however the names that are used and
    > are just from history are very confusing.
    > .
    > A Shared Class is nothing more as a module you could have written it as well
    > in VB as this.
    >
    > Public Module MyThreadCount
    > Private As New Object
    > Private m_threadcount As Int32 = 0
    > Public Sub Sub Increment()
    > SyncLock (m_lock)
    > m_threadcount += 1
    > End SyncLock
    > End Sub
    >
    > etc.
    >
    > A shared class is called in past a static class. In mine opinion is the name
    > completely false.
    >
    > It is a class that is direct used as a module, it has no constructor (Sub
    > New).
    >
    > I hope this gives an idea?
    >
    > Cor
    >
    >
     
    cj, Jul 6, 2006
    #18
  19. cj wrote:
    > Yes, that helps. Just another case of confusing names and terms.
    >
    > I'm still wondering can a class have shared methods and have instances
    > of it?


    Yes. When you declare a Class, think of it creating a single object of
    that type that will be the template for all others. It's an object in
    and of itself. Clear your mind of thread-safe because it's not
    relevant to Shared vs. Instance at this moment.

    Public Class Foobar

    Public Shared Shared_N as Integer
    Public Instance_N as Integer

    Public Shared Sub FooShared()
    Shared_N = Shared_N + 1
    End Sub

    Public Sub BarInstance()
    Instance_N = Instance_N + 1
    End Sub

    Public Sub Hybrid()
    Instance_N = Instance_N + 1
    Shared_N = Shared_N + 1 ' An instance can access shared variables
    End Sub
    End Class

    I can then do the following:

    Foobar.FooShared()
    System.Console.WriteLine(Foobar.Shared_N)

    But what if I create an instance? Try this:

    Dim f as New Foobar

    f.FooShared()
    System.Console.WriteLine(f.Shared_N)
    System.Console.WriteLine(Foobar.Shared_N) '' <== EXACTLY the same
    memory space as f.Shared_N

    f.FooShared() '' <== EXACTLY the same method is called as
    Foobar.FooShared()

    A shared member (field or method) can be called straight on the class
    object. It can also be called or referenced by instance methods.
     
    Travers Naran, Jul 7, 2006
    #19
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Hubbard

    Microsoft MVPs Say They Want Old VB Back

    Jim Hubbard, Mar 12, 2005, in forum: Microsoft VB .NET
    Replies:
    194
    Views:
    1,936
    Guest
    Apr 7, 2005
  2. Replies:
    1
    Views:
    234
    Joanna Carter [TeamB]
    Jan 20, 2006
  3. fiaolle
    Replies:
    0
    Views:
    136
    fiaolle
    Jul 21, 2006
  4. Guest

    What exactly does this message mean?

    Guest, Aug 27, 2006, in forum: Microsoft VB .NET
    Replies:
    7
    Views:
    174
    Jim Wooley
    Aug 28, 2006
  5. Jeff
    Replies:
    0
    Views:
    243
Loading...

Share This Page