Newbie class inheritance question

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Newbie class inheritance question

    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 Guest

  2. Similar Questions and Discussions

    1. Screen Class Inheritance
      I am trying to create a form application in Flash MX2004. I have created a class and set it as the class for the form. I seem to be having trouble...
    2. NEWBIE question: web reference to a proxy class
      Hi, I have used wsdl.exe to get the proxy class from a wsdl document that is saved locally on my comp. Can anyone please tell me how I can add a...
    3. Newbie Class variable question
      Hello! Forgive my newbie-ness. Using ruby 1.8, while trying to create a similar example of one I found to the book called "The Ruby Way": ...
    4. Class inheritance behaviour
      Just double checking with the php crowd. Code example: ------------- <?php $bar = new second_class; echo $bar->foo() . '<br />';...
    5. Class inheritance errors
      I seem to have missed something somewhere, but I can't seem to find a way to make the following work properly, and I think it should. (This is...
  3. #2

    Default Re: Newbie class inheritance question

    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
    [email]john.saunders@surfcontrol.com[/email]

    "Jon" <to_jon@hotmail.com> wrote in message
    news:e4b2505c.0308061218.513521ee@posting.google.c om...
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139