473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Shadows and Overrides and Bears, Oh My!

I am developing a VB.NET app using Visual Studio.NET 2003. VB.NET allows
me to create a class with two or more methods that have the same name, as
long as they have different (non-optional) arguments, such as:

FirstClass
Public Sub Populate(string)
Public Sub Populate(string, string)

I don't have to use Overloads, because both methods are in the same class.

I also understand that through inheritance, I can have a class that
derives from a base class, and the derived class inherits methods:

BaseClass
Public Sub Populate(string)

FirstClass
(Has Populate(string) available)

Now, if I need to replace the Populate method in another class, I use
Protected and Overrides:

BaseClass
Protected Overridable Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overrides Sub Populate(string)
* different code here *

Through what I *think* is polymorphism, I can have another class call the
Populate method of either FirstClass or SecondClass:

OtherClass
PopulateClass(a as BaseClass, mystring as String)
a.Populate(mystring)

PopulateClass can be passed either a FirstClass or a SecondClass object, and
the populate method will be called.

Now, what I want to do is have two methods with the same name but
different signatures, such as:

BaseClass
Protected Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overloads? Overrides? Shadows? Sub Populate(string)
Populate Overloads? Overrides? Shadows? Sub Populate(string, integer,
integer)

The trouble is, I can't figure out whether to use Shadows, Override or
Overloads. I get an error if I only use Shadows on one method, and the
*base* method is called if I use Overrides or Overloads. Can someone help
me out here?

--
Christopher W. Douglas
SRS Technologies, Inc.
christopher (dot) douglas (at) srs (dot) com
Jul 21 '05 #1
4 2085
Use Overloads when you want to create multiple methods which differ only in
the parameters

Use Overrides, when you have a overridable method in a base class and you
want to circumvent that in the derived class, using the same method signature

Use Shadows when you have a non-overridable/overridable method in a base
class and you want to circumvent that with a new method signature (change in
access specifiers as well)

"Christopher W. Douglas" wrote:
I am developing a VB.NET app using Visual Studio.NET 2003. VB.NET allows
me to create a class with two or more methods that have the same name, as
long as they have different (non-optional) arguments, such as:

FirstClass
Public Sub Populate(string)
Public Sub Populate(string, string)

I don't have to use Overloads, because both methods are in the same class.

I also understand that through inheritance, I can have a class that
derives from a base class, and the derived class inherits methods:

BaseClass
Public Sub Populate(string)

FirstClass
(Has Populate(string) available)

Now, if I need to replace the Populate method in another class, I use
Protected and Overrides:

BaseClass
Protected Overridable Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overrides Sub Populate(string)
* different code here *

Through what I *think* is polymorphism, I can have another class call the
Populate method of either FirstClass or SecondClass:

OtherClass
PopulateClass(a as BaseClass, mystring as String)
a.Populate(mystring)

PopulateClass can be passed either a FirstClass or a SecondClass object, and
the populate method will be called.

Now, what I want to do is have two methods with the same name but
different signatures, such as:

BaseClass
Protected Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overloads? Overrides? Shadows? Sub Populate(string)
Populate Overloads? Overrides? Shadows? Sub Populate(string, integer,
integer)

The trouble is, I can't figure out whether to use Shadows, Override or
Overloads. I get an error if I only use Shadows on one method, and the
*base* method is called if I use Overrides or Overloads. Can someone help
me out here?

--
Christopher W. Douglas
SRS Technologies, Inc.
christopher (dot) douglas (at) srs (dot) com

Jul 21 '05 #2
ANSWER at end

The Overloads is your tricky wicket here with polymorphism. Late binding
just makes it more complex. Here is some sample code that might illustrate
what you are going through. Class3 is Shadows, Class4 is Overloads. You will
see it does not really matter much.

----------------------------------------------------------------------
Module Module1

Sub Main()

Dim a As New Class1
Dim b As New Class2
Dim c As New Class3
Dim d As New Class4
Dim someString As String = "some string"

Dim pop As New Populator

pop.PopulateClass(a, someString)
Console.WriteLine(" ")
pop.PopulateClass(b, someString)
Console.WriteLine(" ")
pop.PopulateClass(c, someString)
Console.WriteLine(" ")
pop.PopulateClass(d, someString)

Console.Read()

End Sub

End Module

Public Class Populator

Public Sub PopulateClass(ByRef a As Class1, ByVal b As String)
Console.WriteLine("Input Type")
Console.WriteLine(a.GetType())

a.Populate(b)

Dim thisType As String = a.GetType().ToString()

Console.WriteLine("Running as own Class Type:")

Select Case (a.GetType().ToString())
Case "Polymorph.Class1"
Dim c As Class1 = CType(a, Class1)
Console.WriteLine(a.GetType())
c.Populate(b)
Case "Polymorph.Class2"
Dim c As Class2 = CType(a, Class2)
Console.WriteLine(a.GetType())
c.Populate(b)
Case "Polymorph.Class3"
Dim c As Class3 = CType(a, Class3)
Console.WriteLine(a.GetType())
c.Populate(b)
Case "Polymorph.Class4"
Dim c As Class4 = CType(a, Class4)
Console.WriteLine(a.GetType())
c.Populate(b)
End Select
End Sub

End Class

Public Class Class1
Public Overridable Sub Populate(ByVal param As String)
Console.WriteLine(param & " - from Class1.Populate(string)")
End Sub

Public Overridable Sub Populate(ByVal param As String, ByVal param2 As
String)
Console.WriteLine(param & " - from Class1.Populate(string, string)")
End Sub

End Class

Public Class Class2
Inherits Class1

End Class

Public Class Class3
Inherits Class1

Public Shadows Sub Populate(ByVal param As String)
Console.WriteLine(param & " - from Class3.Populate(string)")
End Sub

End Class

Public Class Class4
Inherits Class1

Public Overloads Sub Populate(ByVal param As String)
Console.WriteLine(param & " - from Class3.Populate(string)")
End Sub

End Class
----------------------------------------------------------------------

The output is as follows:

----------------------------------------------------------------------
Running as own Class Type:
Polymorph.Class1
some string - from Class1.Populate(string)

Input Type
Polymorph.Class2
some string - from Class1.Populate(string)
Running as own Class Type:
Polymorph.Class2
some string - from Class1.Populate(string)

Input Type
Polymorph.Class3
some string - from Class1.Populate(string)
Running as own Class Type:
Polymorph.Class3
some string - from Class3.Populate(string)

Input Type
Polymorph.Class4
some string - from Class1.Populate(string)
Running as own Class Type:
Polymorph.Class4
some string - from Class4.Populate(string)

----------------------------------------------------------------------

Now, here is the way to solve this tricky problem:

1. Turn Option Strict Off - as much as I loath this, there are times it is
necessary

Option String Off 'This goes as top of page

2. Change the PopulateClass() signature

Public Sub PopulateClass(ByRef a As Object, ByVal b As String)

Run the program again and this is the output:

----------------------------------------------------------------------
Input Type
Polymorph.Class1
some string - from Class1.Populate(string)
Running as own Class Type:
Polymorph.Class1
some string - from Class1.Populate(string)

Input Type
Polymorph.Class2
some string - from Class1.Populate(string)
Running as own Class Type:
Polymorph.Class2
some string - from Class1.Populate(string)

Input Type
Polymorph.Class3
some string - from Class3.Populate(string)
Running as own Class Type:
Polymorph.Class3
some string - from Class3.Populate(string)

Input Type
Polymorph.Class4
some string - from Class4.Populate(string)
Running as own Class Type:
Polymorph.Class4
some string - from Class4.Populate(string)
----------------------------------------------------------------------

Overloads or Shadows? In this case, it is not as important, as a call to
MyBase.Populate(param)

Will still yield:
some string - from Class1.Populate(string)

Overall, I would choose Shadows, as the Overload is identical to the
underlying base implementation. I do not believe this will work in other
..NET langauges. Also, you should really get used to Reflection if there is
ever a chance of programming in another .NET language, as Reflection works
in all languages and Late Binding only works in VB.NET.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Christopher W. Douglas" <ch********************@SPAM.srs.com> wrote in
message news:%2***************@TK2MSFTNGP12.phx.gbl...
I am developing a VB.NET app using Visual Studio.NET 2003. VB.NET allows me to create a class with two or more methods that have the same name, as
long as they have different (non-optional) arguments, such as:

FirstClass
Public Sub Populate(string)
Public Sub Populate(string, string)

I don't have to use Overloads, because both methods are in the same class.
I also understand that through inheritance, I can have a class that
derives from a base class, and the derived class inherits methods:

BaseClass
Public Sub Populate(string)

FirstClass
(Has Populate(string) available)

Now, if I need to replace the Populate method in another class, I use
Protected and Overrides:

BaseClass
Protected Overridable Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overrides Sub Populate(string)
* different code here *

Through what I *think* is polymorphism, I can have another class call the Populate method of either FirstClass or SecondClass:

OtherClass
PopulateClass(a as BaseClass, mystring as String)
a.Populate(mystring)

PopulateClass can be passed either a FirstClass or a SecondClass object, and the populate method will be called.

Now, what I want to do is have two methods with the same name but
different signatures, such as:

BaseClass
Protected Sub Populate(string)

FirstClass
(Has Populate(string) available)

SecondClass
Public Overloads? Overrides? Shadows? Sub Populate(string)
Populate Overloads? Overrides? Shadows? Sub Populate(string, integer,
integer)

The trouble is, I can't figure out whether to use Shadows, Override or
Overloads. I get an error if I only use Shadows on one method, and the
*base* method is called if I use Overrides or Overloads. Can someone help
me out here?

--
Christopher W. Douglas
SRS Technologies, Inc.
christopher (dot) douglas (at) srs (dot) com

Jul 21 '05 #3
Cowboy and Rakesh,

Thanks for the responses, but I don't think they answered my basic question.
I have one method in the base class that all derived classes need to be able
to replace, and I want to be able to create other methods with the same name
in the derived classes with different signatures.

I want to be able, without Late Binding and without Option Strict Off, to
have a base class method Populate, which is always overriden by the derived
class. I also want to have other methods named Populate that have different
signatures, so the code knows which one to use based on the arguments
provided.

I want to have another class (or form) with a method that has the base class
as an argument:

DoMyPopulatin(MyObject as BaseObject, a as String)
MyObject.Populate(a)

Then, within each of the derived classes, Populate is replaced with database
code specific to that object. If there is only ONE populate method, I make
it overrides in the derived class:

FirstClass
Public Overrides Sub Populate(a as String)

and everybody is happy. However, if there are TWO populate methods (and I
don't want to make a populate method in the base class for every possible
permutation of derived classes):

FirstClass
Public Overrides Sub Populate(a as String)
Public Shadows Sub Populate(a as String, b as String)

gives me the error: Populate cannot override a method that has been
shadowed. While this:

FirstClass
Public Overrides Sub Populate(a as String)
Public Shadows Sub Populate(a as String, b as String)

gives me the error: sub 'Populate' shadows an overloadable member declared
in the base class 'BaseClass'. If you want to overload the base method,
this method must be declared 'Overloads'. Now, this:

FirstClass
Public Shadows Sub Populate(a as String)
Public Shadows Sub Populate(a as String, b as String)

or this:

FirstClass
Public Overloads Sub Populate(a as String)
Public Overloads Sub Populate(a as String, b as String)

does not give any errors, but the BASE method is always executed under:

DoMyPopulatin(MyObject as BaseObject, a as String)
MyObject.Populate(a)

Am I asking too much? :^)

I really don't want to use CType or a select statement or anything like
that, because then why bother creating a base class?
--
Christopher W. Douglas
SRS Technologies, Inc.
christopher (dot) douglas (at) srs (dot) com
"Cowboy (Gregory A. Beamer) [MVP]" <No************@comcast.netNoSpamM> wrote
in message news:#V**************@tk2msftngp13.phx.gbl...
ANSWER at end

The Overloads is your tricky wicket here with polymorphism. Late binding
just makes it more complex. Here is some sample code that might illustrate
what you are going through. Class3 is Shadows, Class4 is Overloads. You will see it does not really matter much.

Jul 21 '05 #4
Rakesh,

AHAH! You gave me the answer. I didn't realize you could have Override
and Overloads in the same function, I thought you had to choose only one (or
none). MustInherit doesn't work for me (I have to use base class objects
too), but this does:

Public Class Base
Public Sub Populate(ByVal a As String)
* No Code in method *
End Sub
End Class

Public Class Derv
Inherits Base

Public Overloads Overrides Sub Populate(ByVal a As String)
End Sub

Public Overloads Sub Populate(ByVal a As String, ByVal b As String)
End Sub
End Class

Using Overloads and Overrides in the same function ensures that the derived
function is called, and an external function like this can be fed a Derv
object:

GetToPopulatin(MyObject as Base, a as String)
MyObject.Populate(a) <---- Calls the Derv class' Populate method
End Sub

Thank you so much for your help.
--
Christopher W. Douglas
SRS Technologies, Inc.
christopher (dot) douglas (at) srs (dot) com
"Rakesh Rajan" <Ra*********@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
That was a veeeeery long post....

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nic | last post by:
Hi, The original creators of the ListView control never envisioned that we'd want to override the Columns property with our own version, so they didn't mark it as Overridable. This means we...
10
by: Özden Irmak | last post by:
Hi, I'm trying to hide an event of my custom usercontrol derived control, something like "Shadows" in VB.Net, but although the event is hidden from PropertyGrid, from CodeEditor I can still...
10
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
7
by: Satish | last post by:
Hi Friends I am little confused about the shadows keyword in VB.NET could anyone explain with an example about Shadows keyword Many thanks Satish
6
by: Jeff Johnson [MVP: VB] | last post by:
I'm developing a form which is to be used as an enhanced MessageBox(). I don't want it to be shown with the default Show() method, so as I provided overloaded versions of Show() I marked them as...
3
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
4
by: Christopher W. Douglas | last post by:
I am developing a VB.NET app using Visual Studio.NET 2003. VB.NET allows me to create a class with two or more methods that have the same name, as long as they have different (non-optional)...
4
by: dbuchanan | last post by:
Is the following behavior normal? Both the 'Protected sub' in the inherited form and the 'Private Shadows sub' in the derived form fires. My interpretation of MSDN help on the topic "Shadows"...
2
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
In the class below, I inherit from Generic.Dictionary so I can override property Item. Item is not overridable, so I used Shadows, and it works as I want. It works equally well if I replace...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.