Home > Coding Helpers > Converting methods to functions or procedures

Converting methods to functions or procedures

October 31st, 2011

The simplest type of method is one that performs a task without requiring any parameters and without returning any information – let’s call them procedures (or void methods). Methods that use some type as the return value – are functions.

Sometimes, when we declare a method with a return value, we may realize that we don’t actually need this return value, and instead, need a simple void method that does not return anything. On the other hand, we marked a method as void, but may require it to be a function, returning a value.

In this case, to help you in converting a void method into a function and vice versa, CodeRush Pro suggests the corresponding code providers:

  • Convert to Function
  • Convert to Procedure

Both are opposites of each other.┬áBefore you apply them – you can see the resulting preview code.

The Convert to Function code provider changes a method that returns void (or a Sub in VB) into a function that returns an appropriate type. The type is inferred from the returning value. This code provider will help you easily add a return type for a method and guess its correct type, for example:

CodeRush Convert to Function preview

The Convert to Procedure code provider converts a private function that returns some value into a method that returns void (or a Sub in VB). It will also change all “return something” statements into simple returns, for example:

CodeRush Convert to Procedure preview

Products: CodeRush Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Oct/31/2011
ID: C141

Similar Posts: