Help required in polymorphism
-
Hi, See my code below. Public Class OOPS Public Function Check(ByVal a As Int32, ByVal b As Int32) As Int16 Return a + b End Function Public Function Check(ByVal a As Int16, ByVal b As Int16) As Int16 Return a - b End Function End Class Dim obj As New OOPS Dim retVal As Int16 retVal = obj.Check(1, 2) Which function will be called the first one or the second one and why. Thanks in advance for your help
Rohan
-
Hi, See my code below. Public Class OOPS Public Function Check(ByVal a As Int32, ByVal b As Int32) As Int16 Return a + b End Function Public Function Check(ByVal a As Int16, ByVal b As Int16) As Int16 Return a - b End Function End Class Dim obj As New OOPS Dim retVal As Int16 retVal = obj.Check(1, 2) Which function will be called the first one or the second one and why. Thanks in advance for your help
Rohan
-
rohan1981 wrote:
Which function will be called the first one or the second one
Neither
rohan1981 wrote:
and why.
Because I won't use Visual Basic
Thats a good one!!:laugh:
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
-
Hi, See my code below. Public Class OOPS Public Function Check(ByVal a As Int32, ByVal b As Int32) As Int16 Return a + b End Function Public Function Check(ByVal a As Int16, ByVal b As Int16) As Int16 Return a - b End Function End Class Dim obj As New OOPS Dim retVal As Int16 retVal = obj.Check(1, 2) Which function will be called the first one or the second one and why. Thanks in advance for your help
Rohan
This is a VB.NET specific question and one that is quite easy to test on your own. All you 'd have to do is put
Debug.WriteLine("Int32 version")
before the
Return
statement in the Int32 version. The default data type for both arguments in yourCheck
call would be Int32. The compiler never assumes that a integer would be an Int16. From there, it's easy to figure out that the Int32 version of your method would get called. The Int16 version would never be called unless you passed in two arguments that were specifically defined as Int16's.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
This is a VB.NET specific question and one that is quite easy to test on your own. All you 'd have to do is put
Debug.WriteLine("Int32 version")
before the
Return
statement in the Int32 version. The default data type for both arguments in yourCheck
call would be Int32. The compiler never assumes that a integer would be an Int16. From there, it's easy to figure out that the Int32 version of your method would get called. The Int16 version would never be called unless you passed in two arguments that were specifically defined as Int16's.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
This is a VB.NET specific question and one that is quite easy to test on your own. All you 'd have to do is put
Debug.WriteLine("Int32 version")
before the
Return
statement in the Int32 version. The default data type for both arguments in yourCheck
call would be Int32. The compiler never assumes that a integer would be an Int16. From there, it's easy to figure out that the Int32 version of your method would get called. The Int16 version would never be called unless you passed in two arguments that were specifically defined as Int16's.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Thannks for the detailed description. I think, I mae a mistake using vb.net code syntax what if i would have put C# syntax to make my point clear?
Rohan
Either way, the language you choose is going to dictate which forum the question should go in. Even though both C# and VB.NET both target the .NET CLR, there are differences between the languages. For instance, VB.NET supports optional parameters in methods, where C# doesn't. C# supports pointers, where VB.NET does not.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Thannks for the detailed description. I think, I mae a mistake using vb.net code syntax what if i would have put C# syntax to make my point clear?
Rohan