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