Methods Differ By Return Type Only - How?

M

Mythran

Attached is the shortest complete program I could come up with that
demonstrates a current problem we are having with some libraries that have
classes that inherit from System.Web.UI.Page, extend it, and then has other
classes inherit from those derived classes.

So, to explain the attached projects...there are 2 solutions...one is a
solution written with Visual Studio .Net 2003 (v1.1) and the other is
written with Visual Studio 2008 (v2.0).

v1.1 Contains TestLibrary and TestLibrary2 projects.
v2.0 Contains TestLibrary and a console project.

v1.1 TestLibrary has a class named BaseClass. v1.1 TestLibrary2 has a class
named DerivedClass which inherits from BaseClass.
v2.0 TestLibrary has a class named BaseClass. The v2.0 console application
project has a class named DerivedClass2 which inherits from DerivedClass.

The console program shows that even though v1.1 TestLibrary2 DerivedClass
inherits from v1.1 TestLibrary, it is actually inheriting from v2.0
TestLibrary (which is fine and what we want). What we don't get is that
both v1.1 TestLibrary2's DerivedClass has a method named GetBaseValue that
returns a string. The v2.0 TestLibrary's BaseClass also has a method named
GetBaseValue that returns an INTEGER. Using reflection, we show that both
methods ARE defined and functional. When we call GetBaseValue directly,
from within the console, without using reflection, the GetBaseValue from
v1.1 TestLibrary2's DerivedClass is called. How does this work? How does
the .Net Framework determine which method to call? There is no
overloading/overriding done here...so what's going on?

Thanks,
Mythran
 
J

\Ji Zhou [MSFT]\

Hello Mythran,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

I open, read and execute your sample projects. I think the result make
senses to me. The following is my understanding,

1.BaseClass has a method named GetBaseValue which returns integer.
2.DerivedClass inherits the BaseClass which has another method named
GetBaseValue which returns a string.
3.DerivedClass2 inherits the DerivedClass and does not have GetBaseValue
method.

In the above scenario, when we use the reflection to iterate through all
methods, we will find both of the BaseClass's GetBaseValue and
DerivedClass's GetBaseValue.

If we do not use the reflection and directly call cls.GetBaseValue method,
since the cls is DerivedClass2 type which does not implement a method named
GetBaseValue, the closest parent class's GetBaseValue will be called. In
this case, the closest parent class which implements the GetBaseValue is
the DerivedClass. So the GetBaseValue returns a string.

If we want to call GetBaseValue method of the BaseClass from the cls
variable, please try to cast it to BaseClass firstly and then call that
method. Code,

Console.WriteLine("GetBaseValue from BaseClass: " & CType(cls,
BaseClass).GetBaseValue())

Please let me know if this explains clearly about your concern. If any
future assistance I can provide from my side, please feel free to let me
know and I will try my best to follow up.

Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

\Ji Zhou [MSFT]\

Hello Mythran,

I am writing to check the status of the issue on your side. Could you
please let me know if my explanation addresses your concern or not? If you
have any questions or concerns, please feel free to let me know. I will be
more than happy to be of assistance.

Have a great day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

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