Overload problem

M

Michele

I can't why the execution of this code returns this output: Person -
Employee Since the type class of e1 is 'employee', I wonder why the compiler
doesn't call the second method of the Test class. I hope someone can help.
Thank you very much.Public Class Person
Overridable ReadOnly Property name() As String
Get
Return "Person"
End Get
End Property
End Class
Public Class Employee
Inherits Person
Overrides ReadOnly Property name() As String
Get
Return "Employee"
End Get
End Property
End Class
Public Class Test
Public Overloads Shared Function print(ByVal p1 As Person)
Return "Person - " & p1.name
End Function
Public Overloads Shared Function print(ByVal p1 As Employee)
Return "Employee - " & p1.name
End Function
End Class
Module ModuleTest
Sub Main()
Dim e1 As Person = New Employee
Console.WriteLine(e1.name)
End Sub
End Module
 
J

Jon Skeet [C# MVP]

Michele said:
I can't why the execution of this code returns this output: Person -
Employee Since the type class of e1 is 'employee', I wonder why the compiler
doesn't call the second method of the Test class. I hope someone can help.

The compiler doesn't know that e1 is an employee - it only knows that
it's a person, because that's what you've told it.

Overloading is only determined at compile time. Overriding is
determined at runtime.
 
M

Michele

Thank you, Jon! (only now I noticed my code is not correct, but you fired
the problem anyway...)
Do you think there is a way to execute at runtime the inherited class code?

By the way, the correct code to test the behaviour:

Console.WriteLine( Test.Print( e1))
And I would like it says: "Employee - Employee" instead of "Person -
Employee"

Thanks
M
 
J

Jon Skeet [C# MVP]

Michele said:
Thank you, Jon! (only now I noticed my code is not correct, but you fired
the problem anyway...)
Do you think there is a way to execute at runtime the inherited class code?

You would need to use virtual methods in some form or other, eg have a
virtual method in Person which Test.Print calls.
 
R

Ravichandran J.V.

Although the type of 'e1' is Person, it obtains the reference of the
Employee class and hence, the result.

Anyway, the code that you have posted here is only reading the property
of the e1 object. Where are you calling the methods of the Test class.
Moreover, both the methods of the Test class are Shared, which means
that you don't have to use an object reference to invoke either. So,
resolving the reference of the objects is really not necessary here.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
M

Michele

Hi J.V.,
sorry! I forgot a bit of code, this is the correct code to test the
behaviour:

Console.WriteLine(Test.Print(e1))
And I would like it says: "Employee - Employee" instead of "Person -
Employee"

Thank you,
Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top