Results 1 to 4 of 4

Thread: overloading a method with different outputs

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    overloading a method with different outputs

    i recently discovered a nice feature called overloading, now the question is can i also do that for outputs? for example i want to call a function that would automatically return the datatype i assign the function to

    right now im geting an error saying "blabla and blabla cant overload eachother because they only differ by return types

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: overloading a method with different outputs

    Overloading has nothing to do with return type. Overloading means that the members have the same name and different numbers and/or types of parameters and that's all. Multiple overloads can return different types but that has nothing to do with the overloading. Consider what you're asking for. How could the system decide which overload to invoke based solely on return type given that there's no requirement that the return value even be used? Let's say that you had two DoSomething methods that returned a String and an Integer. If I did this:
    vb.net Code:
    1. DoSomething()
    which one am I calling?

    You might be able to use a generic method but then you only have so much control over what types it can return. For instance, there's no way to restrict it to return just an Integer or String. Basically, you should simply be implementing multiple methods. What benefit would overloading be over that anyway?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    Re: overloading a method with different outputs

    too bad, i figured it would look neat for reading memory and not having to do a byte conversion on the recieving end whether im reading an int or float etc, i guess i could write them out as "readbyte, readfloat" etc tho but it could take alot of writing since i also overloaded the input types into 3 varietys
    Last edited by hazarada; Aug 2nd, 2010 at 09:04 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: overloading a method with different outputs

    It's not really going to take a lot of writing because, even if it did work the way you wanted, you'd still have to write out every overload. All this means is that you will have to add one extra word (about 4 or 5 characters) to each method. Everything esle will be exactly the same as it would if you could overload this way. For an example of exactly what you're looking at, check out the the BinaryReader and BinaryWriter classes. The BinaryWriter has one Write method that is overloaded to accept an argument of any of the basic types, while the BinaryReader class has multiple methods dedicated to one type each, e.g. ReadInt32 and ReadByte.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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