0

Can you pls tell me why I get the error when I use the OVERLOADS keyword in the below code? The error I am getting is as follows:

enter image description here

Module Module1
    Sub Main()
        Dim name As String = "Sougata"
        Dim age As Integer = 36
        Console.WriteLine("Name before Age: {0} ", test(name, age))
        Console.WriteLine("Age before Name: {0}", test(age, name))
        Console.ReadLine()
    End Sub
    Overloads Function test(ByVal name As String, ByVal age As Integer) As Integer
        Return 1
    End Function
    Overloads Function test(ByVal a As Integer, ByVal n As String) As Integer
        Return 2
    End Function

End Module


Sougata
  • 319
  • 1
  • 10
  • 5
    Because you're in a module. Try removing the Overloads keyword. – LarsTech Dec 18 '20 at 17:41
  • 2
    What function, where, are you trying to Overload? What is your aim? Since your Module is not inheriting from anything, it can't Overload anything. If you're just trying to create two signatures for your test function, you can simply declare them with different argument types and you're done. – SteveCinq Dec 18 '20 at 17:41
  • @LarsTech ....if you follow the link (https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/overloads) you shall see that the Declaration Context is defined as >>> "You can use Overloads only in a property or procedure declaration statement.". There is no mention that it cannot be declared inside a module. Hence tried the same. Now, does this mean that only when I overload a procedure inside a class, can I use the OVERLOADS keyword? If not, then what is the rule to follow when using it? Pls help. – Sougata Dec 18 '20 at 17:58
  • @SteveCinq...requesting your attention to the above comment as I couldn't tag you there......Also, there was no particular functional goal of the code. I was trying to understand overloading of procedures and the usage rules of the OVERLOADS keyword..the point mentioned in the above link I shared is that if I use Overloads with one of the functions then I have to do the same while declaring overloaded versions. I did that...but I dont understand why using the OVERLOADS function inside a MODULE is creating a problem!!!! What am I missing? – Sougata Dec 18 '20 at 18:00
  • @Sougata Refer to my answer regarding method signatures, but also, you need to read up on overloading. I'd have to replicate all of the doco to explain it in detail. Essentially, you use `Overload` to "replace" a method or property in an inherited class. I'll leave it for you to discover the rest. – SteveCinq Dec 18 '20 at 18:09
  • 3
    See [Overloads in a Module](https://learn.microsoft.com/en-us/dotnet/visual-basic/misc/bc42028). – Idle_Mind Dec 18 '20 at 18:55
  • 1
    also check out some really good explanation about the usage of this keyword. In the vast majority of cases the keyword is optional. there are some edge cases in which it is necessary. If you're just learning I'd recommend skipping trying to use this keyword and focus on learning the concept of overloading independently of the keyword. https://stackoverflow.com/questions/4038032/is-the-overloads-keyword-ever-required-in-vb-net – Jeremy Dec 18 '20 at 19:24
  • @Jeremy You *can* omit the `Overloads` directive, but you'll receive a warning if you do. And, IMHO, you should use it if that's what you're doing, for code readability if nothing else. – SteveCinq Dec 18 '20 at 19:50

2 Answers2

0

You're possibly trying to create different function (aka "method") signatures, in which case, you don't need Overloads. You can simply declare the different signatures:

Function test(ByVal name As String, ByVal age As Integer) As Integer
    Return 1
End Function

Function test(ByVal a As Integer, ByVal n As String) As Integer
    Return 2
End Function

Function test(ByVal b As Boolean, ByVal i As Integer, ByVal n As String) As Integer
    Return 3
End Function

Etc.

If you're really looking to implement method Overloading, here's a basic example:

Class BaseClass
    Function Test() As Integer
        Return 1
    End Function
End Class

Class SubClass
    Inherits BaseClass
    Overloads Function Test() As Integer
        Return 2
    End Function
End Class
SteveCinq
  • 1,920
  • 1
  • 17
  • 22
  • 1
    Actually, both of your scenarios are overloading scenarios. I personally like to think that such class methods will get an additional compiler-generated "thisArg"-parameter behind the scenes. That parameter is used to pass a reference to the class instance into the method, to which the `Me` and `MyBase` keywords within the method will refer to. So, as I see it, both `Test` methods here exist within the same scope, but will have a different signature: one takes a "thisArg"-parameter of type `BaseClass` and the other takes a "thisArg"-parameter of type `SubClass`. – Bart Hofland Dec 19 '20 at 08:52
  • I also think you might be confusing overloading with overriding. The big question is what is expected to happen when a reference to a class instance is created like this: `Dim X As BaseClass = New SubClass()` and then `X.Test()` is called. In your code with *overloading*, you will get `1`, because `X` is of type `BaseClass` and thus `BaseClass.Test()` will be called. For polymorphism (returning `2` because the actual instance is a `SubClass` instance), *overriding* should be applied, using the `Overridable` keyword with `BaseClass.Test()` and the `Overrides` keyword with `SubClass.Test()`. – Bart Hofland Dec 19 '20 at 09:04
  • 1
    @BartHofland I'm responding to the OPs use of the `Overloads` keyword, not debating the use or suitability, etc, of it in context. Outside of that, the functionality of the various keywords can be, I agree, somewhat confusing. – SteveCinq Dec 19 '20 at 18:09
0

The Overloads keyword is somewhat peculiar indeed.

Strictly speaking, the Overloads keyword is always optional. And within modules it is not allowed at all. Because of that, and because the overloading intention is already clear when specifying multiple methods with the same name, I tend to think that the Overloads keyword only causes "code pollution", so I would propose to simply never specify it.

As SteveCinq already pointed out in his comments, you will get a compiler warning when overloading a base class' property or method in a derived class. IMHO, that warning is quite useful, because you actually might not get the behavior you want with overloading in such a case. If you need runtime polymorphism with class inheritance, you should use property/method overriding instead of overloading. But in case overloading a base class' property or method in a derived class is actually intended, explicitly specifying the Overloads or Shadows keyword is fine, of course.

Bart Hofland
  • 3,700
  • 1
  • 13
  • 22