Results 1 to 9 of 9

Thread: [RESOLVED] Function overload?

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Resolved [RESOLVED] Function overload?

    Anyone knows how to solve this?

    function 'Validate' shadows an overloadable member declared in the base class 'ContainerControl'. If you want to overload the base method, this method must be declared 'Overloads'.

    While it doesn't really do much to the data that is sent to the database, it still bugs me that there is that kind of error in the program. Here's the code where the overload manifests:

    Code:
    Private Function Validate(ByVal patrontypename As String) As Boolean
    
            If patrontypename = "" Then
                MsgBox("Please fill up all of the required fields.", MsgBoxStyle.Information, "Required Fields Missing")
                txtPatronTypeName.Focus()
                dbConn.Close()
                Return False
            Else
                Return True
            End If
    
        End Function
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function overload?

    Did you try adding the keyword overloads to your function declaration?

    Code:
    Private Overloads Function Validate(ByVal patrontypename As String) As Boolean
    
            If patrontypename = "" Then
                MsgBox("Please fill up all of the required fields.", MsgBoxStyle.Information, "Required Fields Missing")
                txtPatronTypeName.Focus()
                dbConn.Close()
                Return False
            Else
                Return True
            End If
    
        End Function

  3. #3

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Function overload?

    Thanks. That worked. Question though, what does the Overload do exactly to the function (like, enabling the function to overload)? And, why was the function overloaded?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function overload?

    Container controls have a validate function already. So if you add a function to some class that inherits from ContainerControl (or any class that already does inherit from that, like a panel or groupbox) and you call it the same name, the compiler/runtime has to know what one you want to call when you call the validate method. Since you are passing a string to yours, that is enough for the compiler to be able to tell the difference (since the base class function either takes no parameters, or a single boolean parameter) the fact that you pass a string is enough info for the runtime to figure out which one you want to call. That is why you only get a warning and not a syntax error preventing you from even running the app.

    So adding the overloads keyword to your function is you telling the compiler "I know the base class already has this method called validate, but I am adding a new implementation of it, which accepts a string as a parameter".

    If you wanted to add a method called validate, and it took either no parameters, or one boolean, it would conflict with the base class, and you would be forced to name your method something else, or declare it with the "shadows" keyword instead of the overloads keyword, which makes it take the place of the base classes version of the method.

  5. #5

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Function overload?

    I see, so that's why. Come to think of it, the program did not have this error when I only had one instance of the Validate function (located in another form), and it only appeared when I reused the function's name in another form.

    Thanks. That was very informative!
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  6. #6
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Function overload?

    Quote Originally Posted by kleinma View Post
    If you wanted to add a method called validate, and it took either no parameters, or one boolean, it would conflict with the base class, and you would be forced to name your method something else, or declare it with the "shadows" keyword instead of the overloads keyword, which makes it take the place of the base classes version of the method.
    Another option is to use the 'Overrides' keyword (which is probably more commonly used 'Shadows') since you often don't want to hide the base class implementation, but just override it in the derived class.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  7. #7

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Function overload?

    So, then... what is the difference of the Overrides keyword from Shadows and Overloads?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  8. #8
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    710

    Re: Function overload?

    'Overrides' indicates that the method is to be called instead of the base class method (which must be marked 'Overridable') when the method call is made via an object of the derived type.

    'Shadows' indicates that the method is to be called instead of the base class method even when the base class method is not marked 'Overridable'. It also forces the compiler to ignore that the base class even has a method of this name.

    'Overloads' is a weakness in the VB syntax. It shouldn't ever be required (C#, C++, and Java don't use a similar keyword), but VB will ask you to use it when you name a derived class method the same as a base class method, but with a different parameter list. It doesn't even correspond one-to-one with the usual meaning of the word 'overload' since VB will not ask you to use it if no base class is involved, unless you've already used it already for another method of the same name. The rules for when it is needed are convoluted.
    Last edited by David Anton; Dec 29th, 2009 at 12:16 AM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  9. #9

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Function overload?

    Thanks you guys, I learned quite a lot from your replies. I'm gonna try applying it to my program then!
    ====================
    ほんとにどもありがとう!

    Rie Ishida

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