Results 1 to 11 of 11

Thread: Overloading

  1. #1

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565

    Overloading

    When I write muliple versions of a method with different parameters, and call the method I will get the choice of which method to use due to overloading.

    However if I wish to have all the methods have the same parameters, but have different return types, will this work in the same way or does overloading only take the parameters lists into account?
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  2. #2

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    (Just found the answer in one of my books)

    The parameter list must differ. Having different return types only will not work.
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The way I have seen and have adopted is to do something like this (from the Data Access Block from MS):

    SqlExecuteReader
    SqlExecuteDataSet
    SqlExecuteScalar

    The first returns a reader, the second a DataSet, and the third a single value. So, if you needed to and another method that was pretty much the same except you wanted to return a different type, you would create one something like this:

    SqlExecuteInt

    Something to think about. Throw in the return type into the method name.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    for what i've read up thats a big missing in vb.net and C# as intermediate language accepts return type overloading
    \m/\m/

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by PT Exorcist
    for what i've read up thats a big missing in vb.net and C# as intermediate language accepts return type overloading
    What do you mean 'big missing'? It isn't supposed to be there. Both C++ and Java do not support this kind of overloading either.

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i mean that in relation to C#'s and vb.net's mother language its a big missing! altough that probably only in 1% of the code we would find a use for it it would be of some use and the guys that developed the languages could have done that :\
    \m/\m/

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    what mother language are you refering to? I would think that since C++ and Java both don't incorporate this, it really isn't missing. It was left out by design.

    I personally think that it shouldn't be there. A method shouldn't return different types, it gets to far away from good programming practice. If it always returns a single type, no matter what the arguments are, it is easier to read and understand. Plus, it greatly reduces errors that are introduced into your programs.

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    their mother language? IL of course!

    hmm i think they could be used..maybe just like c# allows pointers with the unsafe keyword or something like that
    \m/\m/

  9. #9

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    hellswraith : why would you consider that bad practice? The principle of it is exactly the same as overloading?

    Imagine you have a method called GetCurrentUser, which takes no parameters. One version of the method could return the username of the user, and another version of the method could return their user id number.

    How is that any different (in theory) than having two methods which take in the same parameters, but of different types?
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here is the simplest way I can explain it:

    public int MyMethod(int i)

    public bool MyMethod(int i)

    Those are the two methods by which are overloaded the way you would like. Now, lets say in code I do this:

    Object myObj = MyMethod(5);

    That is PERFECTLY legal code, yet, the compliler wouldn't have a clue as to which method I called. That is the simplest way I can explain it.

    Now I know some will say, well why don't you make the languages a little more restrictive so they had to have the correct return type variable being assigned. This sounds good in theory, but then you ruin a lot of what OO is like polymorphism.

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I completely agree with hellswraith...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width