Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 22nd, 2004, 06:10 AM
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Overloading & Overriding The Same Property

In chapter 6 page 180 and 181 I am lost on a concept.

The pages cover the ability to specify the @Overload and the 'Override' function on a property. I can't grasp this concept and need somebody to explain it to me.

I understand an 'Overload' will create another occurance of a
property with a different parameter signature.

I understand an 'Override' will create an occurance of a property with the same parameter signature as a property in the base class and
effectively replace the inherited property.

What I don't understand, is how a property can be overloaded and overriden at the same time. Since the parameter signature of the 'Overload' must be different to one defined in either the base class or the child class and the with an 'Override' the parameter list must be the same as a property defined in the base class.

The two don't seem to be able to co-exist to me.



 
Old August 22nd, 2004, 08:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Override is often used to override the base class implementation. You could have two methods with the same name and different signatures. This is the overloaded functionality. So basically, you overload the methods in the current class, while this method completely overrides the base class functionality.

For example

Public Class Base
  Public Sub Test()
  End Sub
End Class

Public Class NewClass
  Inherits Base

  'Test in Base no longer exists
  Public Overrides Overloads Sub Test()
  End Sub

  'Two test methods exist in the current class, which is why overloads is declared;
  'Overloads doesn't need to be defined in VB.NET; however, even if you take it
  'out, it is still overloading the methods
  Public Overrides Overloads Sub Test(intValue As Int)
  End Sub
End Class

Brian
 
Old August 23rd, 2004, 08:43 AM
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmmm.

Ok my basic understanding of how overide and overload works must be wrong.
Let me explain my understanding again to make sure I have it right.

A base class exists with a one parameter method:-

Public Class Base
  Public Sub Test(ByVal Param1 As String)
  End Sub
End Class

Consider my derived/child class has two occurences of the method by the same name.

The first uses the same parameter list as the base definition and so therefore must be overriden BUT does not need the Overload statement because this derived
occurence does not use overloading:-

Public Class Base
  Public Overridable Sub Test(Byval Param1 As String)
  End Sub
End Class

Public Class NewClass
  Inherits Base

  Public Overrides Sub Test(ByVal Param1 As String)
        ' Different processing to Base without any overloading
  End Class

The second occurence has a different set of parameters so is an overload.
It is not overriding the original base class because its parameter signature is unique.

Public Overrides Sub Test(ByVal Param1 As String, ByVal Param2 as String)
      ' Processing to handle two parameter variation
End Class



The way you describe your example makes me believe that 'Overloads' and 'Overrides' must exist together in the same occurance of a derived class method when an Override occurs?











 
Old August 23rd, 2004, 09:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

If you don't have a Test method with the same signature as the base, that's OK too; that's an overloaded example also:

Public Class Base
  Public Overridable Sub Test(Byval Param1 As String)
  End Sub
End Class

Public Class NewClass
  Inherits Base

  Public Overrides Sub Test(ByVal Param1 As String, ByVal Param2 as String)
      ' Processing to handle two parameter variation
  End Class
End Class

That is an overload. Overload is more for multiple signatures, and overrides is more for inheritance, where you override an existing method signature. The two are not interdependent.

For the two-parameter method signature, you may not need the Overrides declaration; I don't remember (because you aren't directly overriding it).

Brian
 
Old August 23rd, 2004, 11:53 AM
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help but one thing still eludes me.

The bit that surprises me is the very last sub Procedure definition
in your first posting:-

'Two test methods exist in the current class, which is why overloads is
'declared;
'Overloads doesn't need to be defined in VB.NET; however, even if you take it
'out, it is still overloading the methods
Public Overrides Overloads Sub Test(intValue As Int)
End Sub


It contains both 'Overrides' and 'Overloads' even though the parameter signature
is different to the base class. If the parameter signature is not the same
as the base class, I understood an 'Override' declaration is not necessary.
The different parameter signature makes it an Overload declaration since the Base has no parameter.

Is this true?

 
Old August 23rd, 2004, 02:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Yes, you don't have to define it (tested it). Actually you can't state it for a method that isn't overriding a base class method. Sorry for any inconvenience.

In addition, if you are overriding a method, you must declare overloads if you have multiple methods with the same name.

Brian
 
Old August 25th, 2004, 07:30 AM
Registered User
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help bmains.

Really appreciate it.

 
Old August 25th, 2004, 07:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Sure no problem.





Similar Threads
Thread Thread Starter Forum Replies Last Post
custom Property Enable & Disable in user control tiks C# 1 August 20th, 2008 09:31 AM
Overloading nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:30 AM
» in content property natmaster CSS Cascading Style Sheets 1 April 17th, 2006 02:12 AM
Overloading Overriding arv1980 VS.NET 2002/2003 2 January 22nd, 2006 12:55 AM
Overriding arv1980 VS.NET 2002/2003 0 January 21st, 2006 09:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.