Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Help required in polymorphism

Help required in polymorphism

Scheduled Pinned Locked Moved Design and Architecture
oophelp
8 Posts 5 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rohan1981
    wrote on last edited by
    #1

    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

    L D 2 Replies Last reply
    0
    • R rohan1981

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • L led mike

        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

        F Offline
        F Offline
        Fatbuddha 1
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • R rohan1981

          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

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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 your Check 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

          R 2 Replies Last reply
          0
          • D Dave Kreskowiak

            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 your Check 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

            R Offline
            R Offline
            rohan1981
            wrote on last edited by
            #5

            Thanks Dave. This is not actually a VB.NET specific question. I mean because I have taken an example with VB.net syntax it doesn't make this problem VB specific. Anyways, thank you for your reply it is really helpful for me.

            Rohan

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              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 your Check 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

              R Offline
              R Offline
              rohan1981
              wrote on last edited by
              #6

              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

              D E 2 Replies Last reply
              0
              • R rohan1981

                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

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • R rohan1981

                  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

                  E Offline
                  E Offline
                  Ed Poore
                  wrote on last edited by
                  #8

                  It would depend on what the compiler (VB.NET or C#) decided to classify 1 or 2 as.  As Dave pointed out it just so happens that both decide to call it Int32 but one could potentially decided to place 1 and 2 in bytes for they cared.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups