Newbie class inheritance question

Discussion in 'ASP .Net' started by Jon, Aug 6, 2003.

  1. Jon

    Jon Guest

    I'm running into a simple issue when instatiating a derived class that
    calls a custom constructor. Here's the type of code found in the
    parent class:

    Public Class parent
    Private _a As Long
    Private _b As Long
    Private _c As String
    .....

    Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
    String)
    _a = a
    _b = b
    _c = c
    EndSub
    EndClass

    And now the derived class:

    Public Class child
    Inherits parent

    ' apparently, i have to redeclare the private vars found in the
    parent???
    Private _a As Long
    Private _b As Long
    Private _c As String
    .....
    ' cannot inherit the constructor, so define identical one here
    Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
    String)
    ' call mybase.new to assign vars
    MyBase.New(a, b, c)
    EndSub
    EndClass

    By calling 'mybase.new' in the child class, I had expected the var
    assignments in the parent to be executed, populating the vars declared
    in the child.
    What I found with the above class definitions is that the child 'var
    _c' is NOT assigned a value.

    I'm sure there's a simple answer, but I'm a beginner. Also, I found
    some MSDN articles on class inheritance in the 'Upgrading to .NET'
    section, but am looking for something that goes deeper. Any idea
    where I can find articles that go into greater depth on the subject of
    class inheritance?

    Thanks... Jon
    Jon, Aug 6, 2003
    #1
    1. Advertising

  2. Jon, "Private" means "Private". Not even a child class can see private
    members. You would need to declare them as protected in the parent in order
    for the child to use them.

    And as a general rule, you should avoid having derived classes access fields
    in the parent class. Let them stay private, but declare protected properties
    to access them:

    Protected Property A as Long
    Get
    Return _a
    End Get
    Set
    _a = Value
    End Set
    End Property

    This way, the parent class is in control of when these fields are set, and
    the values to which it may be set. For instance, you can put code in the Set
    accessor to validate the value, or even put code in the Get accessor to get
    the value from somewhere else. If you allow your child classes to access the
    fields directly, then the fields will _always_ have to be there, just the
    way they are, or you'll break the derived classes.
    --
    John Saunders
    Internet Engineer


    "Jon" <> wrote in message
    news:...
    > I'm running into a simple issue when instatiating a derived class that
    > calls a custom constructor. Here's the type of code found in the
    > parent class:
    >
    > Public Class parent
    > Private _a As Long
    > Private _b As Long
    > Private _c As String
    > ....
    >
    > Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
    > String)
    > _a = a
    > _b = b
    > _c = c
    > EndSub
    > EndClass
    >
    > And now the derived class:
    >
    > Public Class child
    > Inherits parent
    >
    > ' apparently, i have to redeclare the private vars found in the
    > parent???
    > Private _a As Long
    > Private _b As Long
    > Private _c As String
    > ....
    > ' cannot inherit the constructor, so define identical one here
    > Public Sub New(ByVal a As Long, ByVal b As Long, ByRef c As
    > String)
    > ' call mybase.new to assign vars
    > MyBase.New(a, b, c)
    > EndSub
    > EndClass
    >
    > By calling 'mybase.new' in the child class, I had expected the var
    > assignments in the parent to be executed, populating the vars declared
    > in the child.
    > What I found with the above class definitions is that the child 'var
    > _c' is NOT assigned a value.
    >
    > I'm sure there's a simple answer, but I'm a beginner. Also, I found
    > some MSDN articles on class inheritance in the 'Upgrading to .NET'
    > section, but am looking for something that goes deeper. Any idea
    > where I can find articles that go into greater depth on the subject of
    > class inheritance?
    >
    > Thanks... Jon
    John Saunders, Aug 6, 2003
    #2
    1. Advertising

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. maxw_cc
    Replies:
    1
    Views:
    3,120
    Martijn van Steenbergen
    Dec 21, 2003
  2. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  3. Al
    Replies:
    2
    Views:
    376
  4. johnsonlau
    Replies:
    1
    Views:
    764
    Kai-Uwe Bux
    Jul 21, 2008
  5. Replies:
    5
    Views:
    124
    DevPlayer
    Oct 15, 2011
Loading...

Share This Page