Statements: return

The return statement returns the control to the calling method.

Syntax

Form 1:

return;

Form 2:

returnexpression;

Discussion

In its first version, the return statement terminates the execution of a method and returns the control to the calling method. This version can be used only within a method being declared with void as its return value. Such methods can't return a value. For example, you use the return statement to leave a method early:

// If the value of the property doesn't change - nothing to do. if ( value == pure SomeProperty ) return; // otherwise remember the new value and ... pure SomeProperty = value; // ... force the component to update its state InvalidateViewState();

In its second version, the return statement additionally evaluates the specified expression and returns its result to the calling method. This version can be used only within a method being declared with a data type other than void as its return value. Such method explicitly expects a value to return. Please note, the resulting data type of the expression should match the return value declaration of the method. For example, you can implement a method to search within a component for a text view containing the specified text:

var string text = ...; // The text to search var Core::View view = FindNextView( null, Core::ViewState[]); // Search in the list of views for the text view with the given text while ( view != null ) { var Views::Text textView = (Views::Text)view; // Is this the desired text view? if (( textView != null ) && ( textView.String == text )) return textView; // If not, continue with the next view view = FindNextView( view, Core::ViewState[ Visible ]); } // No matching view found return null;