see below
Quote:
> I am getting some strange behavior from the following test code.
> I am trying to get only instances of the same class (i.e. track
> instances) to share a common Shared variable (in this case c).
> The variable c declared in the base class is a dummy variable so
> that property Count and method CountUp can compile. The derived
> classes shadow the base class' variable c so that it can be
> overridden and converted into a shared variable. The requirement
> is that variable c needs to be shared only among like instances
> (i.e. not between instances of track and sections). And the
> second requirement is that the methods and properties
> implementations on this variable must be in the base class. The
> desired result should print out 1, 1, 0 but instead I get a 1,
> 0, 0. Any ideas?
> Public Sub Main()
> Dim a As New track()
> a.CountUp()
> MsgBox(a.Count())
> Dim b As New track()
> MsgBox(b.Count())
> Dim c As New section()
> MsgBox(c.Count())
> End Sub
> Public Class base
> Protected c As Integer
> Public ReadOnly Property Count()
> Get
> Return c
> End Get
> End Property
> Public Sub CountUp()
> c += 1
> End Sub
> End Class
> Public Class track
> Inherits base
> Private Shared Shadows c As Integer
> End Class
> Public Class section
> Inherits base
> Private Shared Shadows c As Integer
> End Class
If you shadow c in track, you have 1 instance variable in base, and 1
shared variable in track. When you create an instance of track and call
countup, CountUp is located in base. Assigning a value to c in the base
class changes the instance member of the base class because the code is
located in the base class.
Afterwards, when you call b.count, it's a new instance of the track class.
Therefore the instance member of the base class is still 0. A.Count returns
a value different from B.Count because A is not the same instance As B and
because you changed the instance member c, not the shared variable.
Same with Section.
A shared variable is shared among all instances of the same class and it's
derived classes. As soon as you shadow the member in the base class by
another member and declare it shared, it's an additional member.
That's why you get 1, 0, 0.
If I understand you right, you want to create classes, each class has it's
shared member but you don't want to write the same code in each class.
Right? Well, I don't think it's possible. To access the correct shared
member, the code that accesses the member must be in the same class or you
have to use the syntax Classname.SharedMember.
But I think I've got a solution for you (I hope that's what you want):
Public Sub Main()
Dim a As New track()
a.c.CountUp()
MsgBox(a.c.Count())
Dim b As New track()
MsgBox(b.c.Count())
Dim c As New section()
MsgBox(c.c.Count())
End Sub
Public Class Counter
Private c As Integer
Public ReadOnly Property Count() As Integer
Get
Return c
End Get
End Property
Public Sub CountUp()
c += 1
End Sub
End Class
Public Class base
Public Shared ReadOnly c As New Counter()
End Class
Public Class track
Inherits base
Public Shared Shadows ReadOnly c As New Counter()
End Class
Public Class section
Inherits base
Public Shared Shadows ReadOnly c As New Counter()
End Class
Result: 1, 1, 0
So you have one shared member for each class and don't have to write the
code in each class.
Armin