This documentation is archived and is not being maintained.

Method Implementation in Components

Visual Studio .NET 2003

Methods are blocks of code that are implemented in your component to provide functionality to the user. They provide the functionality that lets your component accomplish specific tasks. Methods are of two types: value-returning and non–value-returning.

In Visual Basic, if a method is required to return a value, it is implemented as a Public Function procedure. If no value is returned, it is implemented as a Public Sub procedure. Methods are declared using the following syntax:

' Visual Basic
Public Function ConvertMatterToEnergy(matter as Integer) As Integer
   Dim energy As Integer
   ' Insert code to convert matter to energy.
   Return energy
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 sub procedures, as Visual Basic does. The method declaration indicates the type of the return value. The void keyword is used if the method does not return a value. The syntax for declaring a C# public method is:

// C#
public int ConvertMatterToEnergy(int matter)
{
   int energy;
   // Insert code to convert matter to energy.
   return energy;
}

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. For C#, use widget w instead of object w.

Typed arguments allow many errors to be caught when compiled. The compiler always catches errors, whereas run-time testing is only as good as the coverage of the test suite.

Overloaded Methods

If you want to allow users of your component to supply different combinations of parameters to a method, provide multiple overloads of the method, using explicit data types. Do not create parameters declared As Object that can contain any data type.

Visual Basic Note   The universal data type in the common language specification is Object rather than Variant. Variant has been removed from the language. For more information, see Universal Data Type Changes in Visual Basic.

For example, the Spin method of a hypothetical Widget object might allow either direct specification of spin direction and speed, or specification of another Widget object from which angular momentum is to be absorbed.

' Visual Basic
Overloads Public Sub Spin( _
   ByVal spinDirection As SpinDirectionsEnum, _
   ByVal revolutionsPerSecond As Double)
   ' Insert code to implement the method.
End Sub
Overloads Public Sub Spin(ByVal driver As Widget) _
   ' Insert code to implement the method.
End Sub

// C#
public void Spin(SpinDirectionsEnum spinDirection, 
double revolutionsPerSecond)
{
   // Insert code to implement the method.
}

public void Spin(Widget driver)
{
   // Insert code to implement the method.
}

Iterative Design

As you implement functionality in your component, it is important to test it in the scenarios where you expect it to be used. Testing often reveals additional overloaded methods that will be needed by users of your component. For example, consider the hypothetical Widget object from the previous section. After testing your Widget object, you might realize that there are situations where you need it to spin, but cannot provide parameters to either of the overloads of the Spin method. You might solve this problem by adding an overload of the Spin method that takes no parameters to be invoked in these cases. Such an example is shown below:

' Visual Basic
Overloads Public Sub Spin()
   ' Insert code to overload the method.
End Sub

// C#
public void Spin()
{
   // Insert code to overload the method.
}

See Also

Implementing Properties, Methods, Members, and Events in Components | Overloaded Properties and Methods

Show: