Out Attribute


Out Attribute

Class

System.Runtime.InteropServices.OutAttribute

Applies To

Parameter

Constructor

     New(  ) 

Properties

None defined

Description

The <Out> attribute defines an outbound parameter, a variation of a ByRef parameter. Memory allocation for ByRef parameters is managed by the caller of the procedure; parameters with the <Out> attribute applied have their memory allocation managed by the procedure itself. No value comes in from the caller; a return value only goes out through the parameter. This makes <Out> parameters far more efficient in remoting (calls across machines) and in web method calls.

Usage at a Glance

Although you can define an out parameter using the <Out> attribute, the Visual Basic compiler does not enforce it. If you fail to assign a value to the out parameter, or if you indicate that the parameter is to be passed by value rather than by reference, the compiler does not generate an error. Care must be taken to ensure that all <Out> parameters are passed ByRef, and that they are explicitly assigned a value before the method exits.

Example

     Imports System     Imports System.Runtime.InteropServices     Public Class Person        Public Name As String        Private standardAge As Integer        Dim standardHeight As Integer        Dim standardWeight As Integer        Public Sub New(fullName As String)           ' ----- Perhaps the initial fields would come from a           '       database, but here they are just hard-coded.           Name = fullName           standardAge = 26           standardHeight = 73           standardWeight = 185        End Sub        Public Sub GetStats(<Out> ByRef currentAge As Integer, _              <Out> ByRef currentHeight As Integer, _              <Out> ByRef currentWeight As Integer)           currentAge = standardAge           currentHeight = standardHeight           currentWeight = standardWeight        End Sub     End Class     Module GeneralCode        Public Sub Main(  )           Dim mrTypical As New Person("John Doe")           Dim hisAge As Integer           Dim hisHeight As Integer           Dim hisWeight As Integer           mrTypical.GetStats(hisAge, hisHeight, hisWeight)           MsgBox(mrTypical.Name & " is " & hisHeight & " inches tall.")        End Sub     End Module 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net