Results 1 to 8 of 8

Thread: Overloaded

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Overloaded

    What exactly is an overloaded method?

    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    As stated in the Help,

    Overloading a procedure means defining it in multiple versions, using the same name but different argument lists. The purpose of overloading is to define several closely related versions of a procedure without having to differentiate them by name. You do this by varying the argument list.

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    In other words

    Code:
    Public Sub Hello()
    
    End Sub
    
    Public Sub Hello(a As String)
    
    End Sub
    
    Public Sub Hello(a As Integer)
    
    End Sub
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    umm well how the heck do you know which one you will be using? And couldnt the code be different in each one? That seems totaly confusing and wrong.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Its the compiler that knows the difference. Yes you can have different code for each. It is the parameter's tht distinguish the difference.

    If you pass nothing to sub Hello, then it will run the routine with no parameter. If you pass a string to it Hell("blah") it will run the one with a string parameter.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You know which one will be used because you know what type of parameters are passed. This is meant to provide different variants of the same method, usually the methods do about the samething but add a little extra to handle different types of parameters.

  7. #7

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    OK i see, the intellisense pops up a scrollbar that lets you choose which one to use, letting you know there are multiple methods with the same name.

    But still, having multiple methods with the same name seems pretty dumb IMO.

    If i had 3 methods which accpeted different parameters and had slightly diferent code it would make much more sense to describe those methods.

    VB Code:
    1. 'instead of
    2. Sub Test(ByVal TheString As String)
    3.  
    4.     End Sub
    5.  
    6. Sub Test(ByVal TheInteger As Integer)
    7.  
    8.     End Sub
    9.  
    10. Sub Test()
    11.  
    12.     End Sub
    13.  
    14. 'it would make more sense to use
    15.  
    16. Sub TestStr(ByVal TheString As String)
    17.  
    18.     End Sub
    19.  
    20. Sub TestInt(ByVal TheInteger As Integer)
    21.  
    22.     End Sub
    23.  
    24. Sub Test()
    25.  
    26.     End Sub

    That way you would know exactly what sub you were calling and exactly what it was supposed to do. But anyway... thanks for the info!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  8. #8
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Why name the methods 3 different names when they are for the same thing, just different implementations?

    Just like the New constructor for an ArrayList - you can create an array list many different ways - but they are all new constructors.

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