User Tools

Site Tools


Method Overloading

Method Overloading is a concept that is not known in VO, but is implemented in the .NET runtime.

It means that you can define multiple methods with the same name in a class. The only difference is that the signature (the parameter list) is different. This is true also for constructors - you can define more than one constructor in a class.

An example:

class MyClass

method Foo( cString as string ) as string

return cString

method Foo( nInt as int ) as string

return nInt:ToString()

end class

The compiler is able to understand which method to use depending on the parameter type, so this code will use both of the methods:

local oClass as MyClass

oClass := MyClass{}
oClass:Foo( "Hi guys" )
oClass:Foo( 1234 )

Please note: a different return type is not a different signature, so this will not compile:

method Foo( cString as string ) as string
method Foo( cString as string ) as int

Of course this applies also to longer parameter lists. Here a sample piece of code that uses overloaded constructors and chains them:

class MyClass
  protect _cFullName as string

constructor( cFullName as string )
  _cFullName := cFullName  
constructor( cSurname as string, cName as string )
  super( cName + " " + cSurname )
method_overloading.txt · Last modified: 2018/01/24 12:56 by wolfgangriedmann