inheritance question 
Author Message
 inheritance question

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



Mon, 31 Jan 2005 21:34:16 GMT  
 inheritance question

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



Mon, 31 Jan 2005 22:28:01 GMT  
 inheritance question
Yes, that is a good solution. Thank you. Now I understand what was
happening.

Since you were so good with this explanation I hope you can explain this new
problem I encountered having to do with the Shadows hey word.

I created a sub class inherited from PictureBox. I overrode its Refresh
method from the base class like so because I wanted my own implementation of
it.

        Private Shadows Sub Refresh()
                ...
                ...
        End Sub

The original Refresh method of the PictureBox base class is declared Public
but I shadowed it with the Private key word in hopes that I could shield it
from its consumers. My own Refresh method is only called from inside the
class based on some events so it should be private but how can I shield the
consumer from calling the Refresh method of the base class since its native
behavior conflicts with my own implementation?

Thanks
Perry


Quote:

> see below

> > 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



Wed, 02 Feb 2005 07:20:35 GMT  
 inheritance question

Quote:
> Yes, that is a good solution. Thank you. Now I understand what was
> happening.

> Since you were so good with this explanation I hope you can
> explain this new problem I encountered having to do with the
> Shadows hey word.

> I created a sub class inherited from PictureBox. I overrode its
> Refresh method from the base class like so because I wanted my
> own implementation of it.

>         Private Shadows Sub Refresh()
>                 ...
>                 ...
>         End Sub

> The original Refresh method of the PictureBox base class is
> declared Public but I shadowed it with the Private key word in
> hopes that I could shield it from its consumers. My own Refresh
> method is only called from inside the class based on some events
> so it should be private but how can I shield the consumer from
> calling the Refresh method of the base class since its native
> behavior conflicts with my own implementation?

I don't think it's possible. Inheritance means that your control "is a"
picturebox. As it's a picturebox, it has a refresh method (like all
controls).

Refresh is overridable. You can override Refresh

    Public Overrides Sub Refresh

so your implementation of Refresh will always be called, but it can be
called from outside, too. Why is this a problem? Can you give your private
sub a different name?

Armin



Wed, 02 Feb 2005 18:13:33 GMT  
 inheritance question
Yes I could give my Refresh a different name but that is not the point. I
want to not allow users to use the native refresh method. If I name mine
something else then they can still call the native one which interferes with
my own surface invalidation scheme.

Maybe I can override the public Refresh and leave it blank so in a way it
would erase the base class implementation of it and then I could have my own
refresh with a different name that is private. That might work.

Thanks
Perry


Quote:

> > Yes, that is a good solution. Thank you. Now I understand what was
> > happening.

> > Since you were so good with this explanation I hope you can
> > explain this new problem I encountered having to do with the
> > Shadows hey word.

> > I created a sub class inherited from PictureBox. I overrode its
> > Refresh method from the base class like so because I wanted my
> > own implementation of it.

> >         Private Shadows Sub Refresh()
> >                 ...
> >                 ...
> >         End Sub

> > The original Refresh method of the PictureBox base class is
> > declared Public but I shadowed it with the Private key word in
> > hopes that I could shield it from its consumers. My own Refresh
> > method is only called from inside the class based on some events
> > so it should be private but how can I shield the consumer from
> > calling the Refresh method of the base class since its native
> > behavior conflicts with my own implementation?

> I don't think it's possible. Inheritance means that your control "is a"
> picturebox. As it's a picturebox, it has a refresh method (like all
> controls).

> Refresh is overridable. You can override Refresh

>     Public Overrides Sub Refresh

> so your implementation of Refresh will always be called, but it can be
> called from outside, too. Why is this a problem? Can you give your private
> sub a different name?

> Armin



Thu, 03 Feb 2005 22:30:42 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Inheritance Question for OOP Gurus

2. Inheritance Question

3. Inheritance question

4. Simple inheritance question

5. simple inheritance question

6. Reply, simple inheritance question

7. WinForm Inheritance Question

8. Control Inheritance question.

9. Inheritance Question

10. Collection inheritance Question

11. Inheritance question

12. Visual Inheritance Question

 

 
Powered by phpBB® Forum Software