setting member variable in child class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Altman

    setting member variable in child class

    I have a class that I want many others inherited off of. I have created a
    variable in the parent class that I want the inherited classes to set. But
    I do not want to set the variable in the constructor. Ex.

    Class A
    protected myVar as string
    END Class

    Class B
    inherits A
    'set the myVar Variable
    myVar = "Class B"

    End Class

    I need Class A to have access to the myVar so I can't use shadows and
    redeclare it. This seems like it should be trivial but I can't figure it
    out. The reason I don't want to use a constructo in class B is because I
    built the overloaded contructors in class A and do not want to override each
    one of them everytime I inherit class A.


    --
    TIA

    Altman



  • Larry Lard

    #2
    Re: setting member variable in child class


    Altman wrote:[color=blue]
    > I have a class that I want many others inherited off of. I have created a
    > variable in the parent class that I want the inherited classes to set. But
    > I do not want to set the variable in the constructor. Ex.
    >
    > Class A
    > protected myVar as string
    > END Class
    >
    > Class B
    > inherits A
    > 'set the myVar Variable
    > myVar = "Class B"
    >
    > End Class
    >
    > I need Class A to have access to the myVar so I can't use shadows and
    > redeclare it. This seems like it should be trivial but I can't figure it
    > out. The reason I don't want to use a constructo in class B is because I
    > built the overloaded contructors in class A and do not want to override each
    > one of them everytime I inherit class A.[/color]

    You're going to have to do that though. You can't inherit constructor
    implementations . You're going to have to have overloaded constructors
    for each class dervied from A - these can call MyBase.New(what ever), if
    you like. Initialize myVar in them.

    --
    Larry Lard
    Replies to group please

    Comment

    • Altman

      #3
      Re: setting member variable in child class

      I was hoping I wouldn't have to do that because I have 4 different
      constructors for class A and didn't want to have to in turn make 4 different
      constructors for class b just so it can set the myvar and then call
      mybase.new().


      "Larry Lard" <larrylard@hotm ail.com> wrote in message
      news:1132074680 .503539.234920@ z14g2000cwz.goo glegroups.com.. .[color=blue]
      >
      > Altman wrote:[color=green]
      >> I have a class that I want many others inherited off of. I have created
      >> a
      >> variable in the parent class that I want the inherited classes to set.
      >> But
      >> I do not want to set the variable in the constructor. Ex.
      >>
      >> Class A
      >> protected myVar as string
      >> END Class
      >>
      >> Class B
      >> inherits A
      >> 'set the myVar Variable
      >> myVar = "Class B"
      >>
      >> End Class
      >>
      >> I need Class A to have access to the myVar so I can't use shadows and
      >> redeclare it. This seems like it should be trivial but I can't figure it
      >> out. The reason I don't want to use a constructo in class B is because I
      >> built the overloaded contructors in class A and do not want to override
      >> each
      >> one of them everytime I inherit class A.[/color]
      >
      > You're going to have to do that though. You can't inherit constructor
      > implementations . You're going to have to have overloaded constructors
      > for each class dervied from A - these can call MyBase.New(what ever), if
      > you like. Initialize myVar in them.
      >
      > --
      > Larry Lard
      > Replies to group please
      >[/color]


      Comment

      • Chris

        #4
        Re: setting member variable in child class

        Altman wrote:[color=blue]
        > I was hoping I wouldn't have to do that because I have 4 different
        > constructors for class A and didn't want to have to in turn make 4 different
        > constructors for class b just so it can set the myvar and then call
        > mybase.new().
        >
        >
        > "Larry Lard" <larrylard@hotm ail.com> wrote in message
        > news:1132074680 .503539.234920@ z14g2000cwz.goo glegroups.com.. .
        >[color=green]
        >>Altman wrote:
        >>[color=darkred]
        >>>I have a class that I want many others inherited off of. I have created
        >>>a
        >>>variable in the parent class that I want the inherited classes to set.
        >>>But
        >>>I do not want to set the variable in the constructor. Ex.
        >>>
        >>>Class A
        >>> protected myVar as string
        >>>END Class
        >>>
        >>>Class B
        >>> inherits A
        >>> 'set the myVar Variable
        >>> myVar = "Class B"
        >>>
        >>>End Class
        >>>
        >>>I need Class A to have access to the myVar so I can't use shadows and
        >>>redeclare it. This seems like it should be trivial but I can't figure it
        >>>out. The reason I don't want to use a constructo in class B is because I
        >>>built the overloaded contructors in class A and do not want to override
        >>>each
        >>>one of them everytime I inherit class A.[/color]
        >>
        >>You're going to have to do that though. You can't inherit constructor
        >>implementatio ns. You're going to have to have overloaded constructors
        >>for each class dervied from A - these can call MyBase.New(what ever), if
        >>you like. Initialize myVar in them.
        >>
        >>--
        >>Larry Lard
        >>Replies to group please
        >>[/color]
        >
        >
        >[/color]

        Why doesn't Class A require that the myVar is set in the 4 Class A
        constructors?

        Chris

        Comment

        • Altman

          #5
          Re: setting member variable in child class

          OK I came up with a work around if anyone cares.
          create a mustoverride method in class A ex (fillClassVaria bles). In each of
          my constructors for class A I will call that fillClassVariab les. In class
          b, in the fillClassVariab les method, fill in the variables I need. This
          makes it so I do not need to recreate my constructors.

          example

          Class A
          protected myVar as string
          protected mustoverride function fillClassVariab les
          public sub new()
          fillClassVariab les
          'do stuff
          end sub
          public sub new(var1)
          fillClassVariab les
          'do stuff
          end sub
          public sub new(var1,var2)
          fillClassVariab les
          'do stuff
          end sub
          END Class

          Class B
          inherits A
          protected overrides function fillclassvariab les
          myVar = "Class B"
          end function

          End Class



          "Larry Lard" <larrylard@hotm ail.com> wrote in message
          news:1132074680 .503539.234920@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          >
          > Altman wrote:[color=green]
          >> I have a class that I want many others inherited off of. I have created
          >> a
          >> variable in the parent class that I want the inherited classes to set.
          >> But
          >> I do not want to set the variable in the constructor. Ex.
          >>
          >> Class A
          >> protected myVar as string
          >> END Class
          >>
          >> Class B
          >> inherits A
          >> 'set the myVar Variable
          >> myVar = "Class B"
          >>
          >> End Class
          >>
          >> I need Class A to have access to the myVar so I can't use shadows and
          >> redeclare it. This seems like it should be trivial but I can't figure it
          >> out. The reason I don't want to use a constructo in class B is because I
          >> built the overloaded contructors in class A and do not want to override
          >> each
          >> one of them everytime I inherit class A.[/color]
          >
          > You're going to have to do that though. You can't inherit constructor
          > implementations . You're going to have to have overloaded constructors
          > for each class dervied from A - these can call MyBase.New(what ever), if
          > you like. Initialize myVar in them.
          >
          > --
          > Larry Lard
          > Replies to group please
          >[/color]


          Comment

          • Altman

            #6
            Re: setting member variable in child class

            OK so this is something I didn't realize. The overloaded constructor does
            not carry down to the inherited class. Is this right? Is this the same in
            C# or J#?


            "Altman" <NotGiven@SickO fSpam.com> wrote in message
            news:%23KU3yFg6 FHA.268@TK2MSFT NGP10.phx.gbl.. .[color=blue]
            >I have a class that I want many others inherited off of. I have created a
            >variable in the parent class that I want the inherited classes to set. But
            >I do not want to set the variable in the constructor. Ex.
            >
            > Class A
            > protected myVar as string
            > END Class
            >
            > Class B
            > inherits A
            > 'set the myVar Variable
            > myVar = "Class B"
            >
            > End Class
            >
            > I need Class A to have access to the myVar so I can't use shadows and
            > redeclare it. This seems like it should be trivial but I can't figure it
            > out. The reason I don't want to use a constructo in class B is because I
            > built the overloaded contructors in class A and do not want to override
            > each one of them everytime I inherit class A.
            >
            >
            > --
            > TIA
            >
            > Altman
            >
            >
            >[/color]


            Comment

            • Altman

              #7
              Re: setting member variable in child class

              >[color=blue]
              > Why doesn't Class A require that the myVar is set in the 4 Class A
              > constructors?
              >[/color]

              Because I have methods of class A that use the myVar but I they are
              different depending on the inherited class. Therefore the inherited class
              needs to set the value of these variables.


              Comment

              • Chris

                #8
                Re: setting member variable in child class

                Altman wrote:[color=blue]
                > OK I came up with a work around if anyone cares.
                > create a mustoverride method in class A ex (fillClassVaria bles). In each of
                > my constructors for class A I will call that fillClassVariab les. In class
                > b, in the fillClassVariab les method, fill in the variables I need. This
                > makes it so I do not need to recreate my constructors.
                >
                > example
                >
                > Class A
                > protected myVar as string
                > protected mustoverride function fillClassVariab les
                > public sub new()
                > fillClassVariab les
                > 'do stuff
                > end sub
                > public sub new(var1)
                > fillClassVariab les
                > 'do stuff
                > end sub
                > public sub new(var1,var2)
                > fillClassVariab les
                > 'do stuff
                > end sub
                > END Class
                >
                > Class B
                > inherits A
                > protected overrides function fillclassvariab les
                > myVar = "Class B"
                > end function
                >
                > End Class
                >
                >
                >
                > "Larry Lard" <larrylard@hotm ail.com> wrote in message
                > news:1132074680 .503539.234920@ z14g2000cwz.goo glegroups.com.. .
                >[color=green]
                >>Altman wrote:
                >>[color=darkred]
                >>>I have a class that I want many others inherited off of. I have created
                >>>a
                >>>variable in the parent class that I want the inherited classes to set.
                >>>But
                >>>I do not want to set the variable in the constructor. Ex.
                >>>
                >>>Class A
                >>> protected myVar as string
                >>>END Class
                >>>
                >>>Class B
                >>> inherits A
                >>> 'set the myVar Variable
                >>> myVar = "Class B"
                >>>
                >>>End Class
                >>>
                >>>I need Class A to have access to the myVar so I can't use shadows and
                >>>redeclare it. This seems like it should be trivial but I can't figure it
                >>>out. The reason I don't want to use a constructo in class B is because I
                >>>built the overloaded contructors in class A and do not want to override
                >>>each
                >>>one of them everytime I inherit class A.[/color]
                >>
                >>You're going to have to do that though. You can't inherit constructor
                >>implementatio ns. You're going to have to have overloaded constructors
                >>for each class dervied from A - these can call MyBase.New(what ever), if
                >>you like. Initialize myVar in them.
                >>
                >>--
                >>Larry Lard
                >>Replies to group please
                >>[/color]
                >
                >
                >[/color]

                After reading that I was wondering if knew you didn't have to "recreate"
                your constructors, you just a have to copy the definitions of the
                contrutors.

                Public Class A
                sub new(1, 2, 3)
                ....
                end sub
                sub new(1, 2, 3, 4)
                ....
                end sub
                end class

                Public Class B
                sub new(1, 2, 3)
                mybase.new(1,2, 3)
                do soemthing else
                end sub
                sub new(1, 2, 3, 4)
                mybase.new(1,2, 3,4)
                do soemthing else
                end sub
                end class

                Comment

                • Kerry Moorman

                  #9
                  Re: setting member variable in child class

                  Altman,

                  That's right, instructors are not inherited.

                  The first line of code in an inherited class's constructor must call the
                  appropriate constructor in the base class: Mybase.New (...)

                  Kerry Moorman


                  "Altman" wrote:
                  [color=blue]
                  > OK so this is something I didn't realize. The overloaded constructor does
                  > not carry down to the inherited class. Is this right? Is this the same in
                  > C# or J#?
                  >
                  >
                  > "Altman" <NotGiven@SickO fSpam.com> wrote in message
                  > news:%23KU3yFg6 FHA.268@TK2MSFT NGP10.phx.gbl.. .[color=green]
                  > >I have a class that I want many others inherited off of. I have created a
                  > >variable in the parent class that I want the inherited classes to set. But
                  > >I do not want to set the variable in the constructor. Ex.
                  > >
                  > > Class A
                  > > protected myVar as string
                  > > END Class
                  > >
                  > > Class B
                  > > inherits A
                  > > 'set the myVar Variable
                  > > myVar = "Class B"
                  > >
                  > > End Class
                  > >
                  > > I need Class A to have access to the myVar so I can't use shadows and
                  > > redeclare it. This seems like it should be trivial but I can't figure it
                  > > out. The reason I don't want to use a constructo in class B is because I
                  > > built the overloaded contructors in class A and do not want to override
                  > > each one of them everytime I inherit class A.
                  > >
                  > >
                  > > --
                  > > TIA
                  > >
                  > > Altman
                  > >
                  > >
                  > >[/color]
                  >
                  >
                  >[/color]

                  Comment

                  Working...