* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
3.3 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Method Implementation in Custom Controls | 03/30/2017 |
|
|
35d14fca-4bb4-4a27-8211-1f7a98ea27de |
Method Implementation in Custom Controls
A method is implemented in a control in the same manner a method would be implemented in any other component.
In Visual Basic, if a method is required to return a value, it is implemented as a Public Function. If no value is returned, it is implemented as a Public Sub. Methods are declared using the following syntax:
Public Function ConvertMatterToEnergy(Matter as Integer) As Integer
' Conversion code goes here.
End Function
Because functions return a value, they must specify a return type, such as integer, string, object, and so on. The arguments Function or Sub procedures take, if any, must also be specified.
C# makes no distinction between functions and procedures, as Visual Basic does. A method either returns a value or returns void. The syntax for declaring a C# public method is:
public int ConvertMatterToEnergy(int matter)
{
// Conversion code goes here.
}
When you declare a method, declare all of its arguments as explicit data types whenever possible. Arguments that take object references should be declared as specific class types — for example, As Widget instead of As Object. In Visual Basic, the default setting Option Strict automatically enforces this rule.
Typed arguments allow many developer errors to be caught by the compiler, rather than at run time. The compiler always catches errors, whereas run-time testing is only as good as the test suite.
Overloaded Methods
If you want to allow users of your control to supply different combinations of parameters to a method, provide multiple overloads of the method, using explicit data types. Avoid creating parameters declared As Object that can contain any data type, as this can lead to errors that might not be caught in testing.
Note
The universal data type in the common language runtime is
Objectrather thanVariant.Varianthas been removed from the language.
For example, the Spin method of a hypothetical Widget control might allow either direct specification of spin direction and speed, or specification of another Widget object from which angular momentum is to be absorbed:
Overloads Public Sub Spin( _
ByVal SpinDirection As SpinDirectionsEnum, _
ByVal RevolutionsPerSecond As Double)
' Implementation code here.
End Sub
Overloads Public Sub Spin(ByVal Driver As Widget) _
' Implementation code here.
End Sub
public void Spin(SpinDirectionsEnum spinDirection, double revolutionsPerSecond)
{
// Implementation code here.
}
public void Spin(Widget driver)
{
// Implementation code here.
}