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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Method Overloading Troubles

Method Overloading Troubles

Scheduled Pinned Locked Moved C#
helptutorialquestionlearning
3 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    Skoder
    wrote on last edited by
    #1

    Hey, i want to do some overloading but i cannot find out how to get the object type to be right. The problem is the following. I have a Base class called Animals, and then i have a couple of sub classes which inherit from the Animal class (etc. Dog, Cat and soo on). I Create my Dog and Cat objects and add them to an ArrayList. For controlling the ArrayList i have a couple of Methods, one of them is GetAnimal();, which will return a object from the ArrayList. But as it is different what type of objects which is returned it seems that i have to return object of type Animal (the object casted). I have a class which computes some movement and stuff. In that class i want to Get an object (GetAnimal()) and call a method, like this. public void Compute() { Move(GetAnimal()); } and then i have Move Methods for every Animal public void Move(Dog dog) { doSomething } public void Move(Cat cat) { doSomething } But my problem is that when i make the GetAnimal() call i get the object as Animal and not as the Correct type needed for overloading. Soo it complains about there is no correct method for overloading. What am i missing / doing wrong ? What should i do to get the object as the correct type which i can use when calling the overload methods I could of course place the methods on the objects and make it abstract instead, but it isnt reall what i want. Hope someone can help me with this. Best regards Martin :confused:

    S 1 Reply Last reply
    0
    • S Skoder

      Hey, i want to do some overloading but i cannot find out how to get the object type to be right. The problem is the following. I have a Base class called Animals, and then i have a couple of sub classes which inherit from the Animal class (etc. Dog, Cat and soo on). I Create my Dog and Cat objects and add them to an ArrayList. For controlling the ArrayList i have a couple of Methods, one of them is GetAnimal();, which will return a object from the ArrayList. But as it is different what type of objects which is returned it seems that i have to return object of type Animal (the object casted). I have a class which computes some movement and stuff. In that class i want to Get an object (GetAnimal()) and call a method, like this. public void Compute() { Move(GetAnimal()); } and then i have Move Methods for every Animal public void Move(Dog dog) { doSomething } public void Move(Cat cat) { doSomething } But my problem is that when i make the GetAnimal() call i get the object as Animal and not as the Correct type needed for overloading. Soo it complains about there is no correct method for overloading. What am i missing / doing wrong ? What should i do to get the object as the correct type which i can use when calling the overload methods I could of course place the methods on the objects and make it abstract instead, but it isnt reall what i want. Hope someone can help me with this. Best regards Martin :confused:

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Time to understand the difference between overloading and overriding. Method overload resolution happens at compile time, so the compiler sees that you call Move with an instance of Animal and that there is no method that takes it as a parameter. Virtual method dispatch, which you can achieve by declaring Move virtual or abstract, happens at run time, based on the runtime type of the object returned by GetAnimal. Because the compiler cannot possibly figure the actual type of the object returned by GetAnimal, it can only assume what you've declared i.e it returns Animal. So no, you cannot do what you want using overloading, unless you return explicit types (GetCat(), GetDog(), for eg.). Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

      S 1 Reply Last reply
      0
      • S S Senthil Kumar

        Time to understand the difference between overloading and overriding. Method overload resolution happens at compile time, so the compiler sees that you call Move with an instance of Animal and that there is no method that takes it as a parameter. Virtual method dispatch, which you can achieve by declaring Move virtual or abstract, happens at run time, based on the runtime type of the object returned by GetAnimal. Because the compiler cannot possibly figure the actual type of the object returned by GetAnimal, it can only assume what you've declared i.e it returns Animal. So no, you cannot do what you want using overloading, unless you return explicit types (GetCat(), GetDog(), for eg.). Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

        S Offline
        S Offline
        Skoder
        wrote on last edited by
        #3

        Thanks for the answer. I decided to go with the abstract solution as i could probably come in handy later on. I guess it would have worked if i used the ArrayList with the objects directly and just did something like Move(theArrayList[index]); -- hmm i am sure i once found an articel on this site about this ... hmm Best regards Martin ... :) -- modified at 22:16 Tuesday 11th April, 2006

        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