issue about output parameter  
Author Message
ConnieLarsen





PostPosted: Top

Net Framework >> issue about output parameter Hi everyone

In real work,some functions have output parameter,it is not necessary for the calling procedure to pass in a parameter that contain non-null value,so a programmer hope passing in a object variable and get back the output value
The following code works well in VB.NET
----------------------------------------------------
Public Function AddNumbers(ByVal NumberOne As Double,
ByVal NumberTwo As Double, ByRef note As String)
As Doubl
AddNumbers = NumberOne + NumberTw
note = "Hello,I'm a calculator.
End Functio

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic

Dim s1 As Objec
MsgBox(AddNumbers(7, 8, s1)
MsgBox(s1

End Su
----------------------------------------------------
But if the called function is in a COM object,using the calling code,just like that in Button1_Click() in VB.NET,will cause exception
In VB.NET,adding a reference to the COM dll,the calling code must be modified like this
------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic

Dim obj As new MyComClas
Dim s1 As string="
MsgBox(obj.AddNumbers(7, 8, s1)
MsgBox(s1

End Su
-------------------------------------------------------------
the critical difference is the declaration on variable s1.If not declaring it as a specific type and not assigning it a explicit value,the calling will fail

AS it is a output parameter,it seems not necessary to pass in a specific value,moreover,it will be more convinent if it can be passed in a object variable.In some case,the calling procedure can not declare the s1 variable as the corresponding date type to that in the declaration of the function
Is there anyone point out a solution to this
An early is appreciated

Best regards
Mary.

DotNet358