Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Overriding Methods Question

Overriding Methods Question

Scheduled Pinned Locked Moved Visual Basic
helpcsharpbeta-testingquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    watagal
    wrote on last edited by
    #1

    Greetings, My hope is that someone can help a newbie trying to learn this OOP on her own. My base class is POINT2 and the derived class is POINT3 and I'm trying to overide the subroutine called add. As the code is written - I get the error sub "add" cannot be declared 'Overrides' because it does not overrides a sub in a base class and it recommends to me to remove the keyword Overrides. Once I remove the keyword Overrides, I get the error message, sub 'add' shadows an overridable method in the base class 'POINT2'. To override the base method, this method must be declared 'Overrides'. Now I'm not pointing the finger at Microsoft, I'm sure it is something I did or didn't do. Help me stop chasing my tail. Karen Nooobie to OOP and VB.Net 2005 (beta) ' 2D POINT =========================================================== Public Class POINT2 Friend _X As Decimal = Nothing Friend _Y As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal) mybase.new() Me._X = x Me._Y = y End Sub ' Constructor Property X() Get Return (_X) End Get Set(ByVal value) _X = value End Set End Property ' Get/Set X Property Y() Get Return (_Y) End Get Set(ByVal value) _Y = value End Set End Property ' Get/Set Y Public Overridable Sub add(ByRef p1 As POINT2) Me._X += p1.X Me._Y += p1.Y End Sub End Class ' 2D POINT Class ' 3D POINT =========================================================== Public Class POINT3 Inherits POINT2 Friend _Z As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) MyBase.New(x, y) Me._X = x Me._Y = y Me._Z = z End Sub ' Constructor Property Z() Get Return (_Z) End Get Set(ByVal value) _Z = value End Set End Property ' Get/Set Z Public **Overrides** Sub

    D 1 Reply Last reply
    0
    • W watagal

      Greetings, My hope is that someone can help a newbie trying to learn this OOP on her own. My base class is POINT2 and the derived class is POINT3 and I'm trying to overide the subroutine called add. As the code is written - I get the error sub "add" cannot be declared 'Overrides' because it does not overrides a sub in a base class and it recommends to me to remove the keyword Overrides. Once I remove the keyword Overrides, I get the error message, sub 'add' shadows an overridable method in the base class 'POINT2'. To override the base method, this method must be declared 'Overrides'. Now I'm not pointing the finger at Microsoft, I'm sure it is something I did or didn't do. Help me stop chasing my tail. Karen Nooobie to OOP and VB.Net 2005 (beta) ' 2D POINT =========================================================== Public Class POINT2 Friend _X As Decimal = Nothing Friend _Y As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal) mybase.new() Me._X = x Me._Y = y End Sub ' Constructor Property X() Get Return (_X) End Get Set(ByVal value) _X = value End Set End Property ' Get/Set X Property Y() Get Return (_Y) End Get Set(ByVal value) _Y = value End Set End Property ' Get/Set Y Public Overridable Sub add(ByRef p1 As POINT2) Me._X += p1.X Me._Y += p1.Y End Sub End Class ' 2D POINT Class ' 3D POINT =========================================================== Public Class POINT3 Inherits POINT2 Friend _Z As Decimal = Nothing Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal) MyBase.New(x, y) Me._X = x Me._Y = y Me._Z = z End Sub ' Constructor Property Z() Get Return (_Z) End Get Set(ByVal value) _Z = value End Set End Property ' Get/Set Z Public **Overrides** Sub

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:

      ' 2D POINT ===========================================================
      Public Class POINT2
      'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
      'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
      ' these to Doubles instead. Decimal types are 128-bit numbers
      ' and a rather slow to work with.
      Friend _X As Double
      Friend _Y As Double
       
      Public Sub New(ByVal x As Double, ByVal y As Double)
      'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
      ' you're calling the New of System.Object, which doesn't
      ' do anything. Remove this line.
      'Me._X = x ' You should be setting your initial values using your
      'Me._Y = y ' Property statements. Your Properties can validate the data
      ' and return Exceptions if not valid.
      Me.X = x
      Me.Y = y
      End Sub ' Constructor
       
      Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
      Get ' Making assumptions and not specifying exactly what you
      Return Me._X ' want Public/Private can lead to hard to find problems.
      End Get
      Set(ByVal value)
      ' You might want to do some checking of your values to see if they
      ' are valid before setting the value of this class instance.
      Me._X = value
      End Set

      W 2 Replies Last reply
      0
      • D Dave Kreskowiak

        In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:

        ' 2D POINT ===========================================================
        Public Class POINT2
        'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
        'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
        ' these to Doubles instead. Decimal types are 128-bit numbers
        ' and a rather slow to work with.
        Friend _X As Double
        Friend _Y As Double
         
        Public Sub New(ByVal x As Double, ByVal y As Double)
        'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
        ' you're calling the New of System.Object, which doesn't
        ' do anything. Remove this line.
        'Me._X = x ' You should be setting your initial values using your
        'Me._Y = y ' Property statements. Your Properties can validate the data
        ' and return Exceptions if not valid.
        Me.X = x
        Me.Y = y
        End Sub ' Constructor
         
        Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
        Get ' Making assumptions and not specifying exactly what you
        Return Me._X ' want Public/Private can lead to hard to find problems.
        End Get
        Set(ByVal value)
        ' You might want to do some checking of your values to see if they
        ' are valid before setting the value of this class instance.
        Me._X = value
        End Set

        W Offline
        W Offline
        watagal
        wrote on last edited by
        #3

        Thanks so much for your quick response. Last week, I read the same thing in 2 different books - you made it much more clearer and practical. Thanks, Karen Nooobie to OOP and VB.Net 2005

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          In your implementation, you're adding functionality to a base class, POINT2, not overriding it. The base class supports every associated with POINT2, including it's Add method. Your Add method takes a parameter of POINT2 pass ByRef. In your POINT3 class, your Add method is taking a parameter of POINT3 ByRef. This is a different method from the base class. So what you're really doing is adding new support to the Add method in POINT2, not replacing it. This is called Overloading. You have multiple methods with the same name that take different parameters. Calling the Add method with a POINT2 parameter will call the POINT2 class' version. Calling the Add method with a POINT3 parameter will call the POINT3 subclass' Add method. A few things need to be changed in your code. We'll start with POINT2:

          ' 2D POINT ===========================================================
          Public Class POINT2
          'Friend _X As Decimal = Nothing ' Remove the "= Nothing"'s. Decimals are value types that
          'Friend _Y As Decimal = Nothing ' can't be set to Nothing. Also, you might want to change
          ' these to Doubles instead. Decimal types are 128-bit numbers
          ' and a rather slow to work with.
          Friend _X As Double
          Friend _Y As Double
           
          Public Sub New(ByVal x As Double, ByVal y As Double)
          'mybase.new() ' Since this class isn't inheriting from anything (well, not really true!),
          ' you're calling the New of System.Object, which doesn't
          ' do anything. Remove this line.
          'Me._X = x ' You should be setting your initial values using your
          'Me._Y = y ' Property statements. Your Properties can validate the data
          ' and return Exceptions if not valid.
          Me.X = x
          Me.Y = y
          End Sub ' Constructor
           
          Public Property X() As Double ' It doesn't hurt to be specific about what your coding.
          Get ' Making assumptions and not specifying exactly what you
          Return Me._X ' want Public/Private can lead to hard to find problems.
          End Get
          Set(ByVal value)
          ' You might want to do some checking of your values to see if they
          ' are valid before setting the value of this class instance.
          Me._X = value
          End Set

          W Offline
          W Offline
          watagal
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

              Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal)
                  'MyBase.New(x, y)                 ' This line isn't required because you're setting all
                                                    ' three values in this constructor.  Anything you do in
                                                    ' in the POINT2 constructor would just be overwritten
                                                    ' by what you have in this one.
                  Me.X = x                          ' Again, go through the Properties!
                  Me.Y = y
                  Me.Z = z
              End Sub ' Constructor
          

          Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints): Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005

          D S 2 Replies Last reply
          0
          • W watagal

            Dave Kreskowiak wrote:

                Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal)
                    'MyBase.New(x, y)                 ' This line isn't required because you're setting all
                                                      ' three values in this constructor.  Anything you do in
                                                      ' in the POINT2 constructor would just be overwritten
                                                      ' by what you have in this one.
                    Me.X = x                          ' Again, go through the Properties!
                    Me.Y = y
                    Me.Z = z
                End Sub ' Constructor
            

            Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints): Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            There's something you haven't shown in the POINT2.New constructor. This message shouldn't come up with the code that I've seen and what I posted. The code that I posted came straight out of VB.NET 2005 and it compiled nicely. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            1 Reply Last reply
            0
            • W watagal

              Dave Kreskowiak wrote:

                  Public Sub New(ByVal x As Decimal, ByVal y As Decimal, ByVal z As Decimal)
                      'MyBase.New(x, y)                 ' This line isn't required because you're setting all
                                                        ' three values in this constructor.  Anything you do in
                                                        ' in the POINT2 constructor would just be overwritten
                                                        ' by what you have in this one.
                      Me.X = x                          ' Again, go through the Properties!
                      Me.Y = y
                      Me.Z = z
                  End Sub ' Constructor
              

              Dave, As soon as I comment out the 'Mybase.New()' statement in POINT3 class, VS2005 complains that it should be there. This is where my code stands (w/o VS complaints): Public Sub New(ByVal x As Double, ByVal y As Double, ByVal z As Double) MyBase.New(x, y) Me.Z = z End Sub ' Constructor Is this ok? Thanks, Karen Nooobie to OOP and VB.Net 2005

              S Offline
              S Offline
              shoaibnawaz
              wrote on last edited by
              #6

              Can you correct this conflict in two methods with similar names but different parameters.

              Public Type MyPoint
                  x As Double
                  y As Double
              End Type
              
              Public Type MyLine
                  Head As MyPoint
                  Tail As MyPoint
              End Type
              
              Public Sub DrawLine(aLine As MyLine, Canves As Form)
                  Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y)
              End Sub
              
              Public Sub DrawLine(aLine As MyLine, Canves As PictureBox)
                  Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y)
              End Sub
              

              Shoaib Nawaz

              D 1 Reply Last reply
              0
              • S shoaibnawaz

                Can you correct this conflict in two methods with similar names but different parameters.

                Public Type MyPoint
                    x As Double
                    y As Double
                End Type
                
                Public Type MyLine
                    Head As MyPoint
                    Tail As MyPoint
                End Type
                
                Public Sub DrawLine(aLine As MyLine, Canves As Form)
                    Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y)
                End Sub
                
                Public Sub DrawLine(aLine As MyLine, Canves As PictureBox)
                    Canves.Line (aLine.Head.x, aLine.Head.y)-(aLine.Tail.x, aLine.Tail.y)
                End Sub
                

                Shoaib Nawaz

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                What conflict? What's the errot you're getting? I see other things wrong that have nothing to do with Overloading, Shadowing, or Overriding. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups