Output Parameters in VB .NET ?

  • Thread starter Thread starter Juan Sutton
  • Start date Start date
J

Juan Sutton

I'm building a web service and trying to get an output similar to
this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add2Response>
<Add2Result>3</Add2Result>
<sum>3</sum>
</Add2Response>
</soap:Body>
</soap:Envelope>

The C# language allows a function like this:

public int Add2(int x, int y, out int sum)
{
sum = x + y;

return sum;
}

Aren't output parameters allowed in VB functions/subroutines? If so,
please provide an example. If there is any other way to return
multiple output variables, please let me know that too.

Thanks
 
specify ByRef instead of ByVal for the parameter.

Public Function Add2(ByVal x As Integer, ByVal y As Integer, ByRef sum As
Integer) As Integer

ByRef works as "ref" paramaters in C#, but just like "out", doesn't require
pre-assignment.

Of course, this example is a bit strange, since the out parameter AND the
function return value are the same thing.

-Rob Teixeira [MVP]
 
Thank you very much. Such a simple solution.

Although, there is one gotcha. That requires the soap request to
include the ByRef parameter. I have no control over the client
behavior so this may be a problem.

Any other ideas are appreciated.
 
Juan,
Have you considered creating a function that returns an object with both
values instead of a function with side effects?

In you are using P/Invoke you can use the OutAttribute to signal the
parameter as output only, I thought there was something similar for Web
methods, however I'm not finding any links right now.

Hope this helps
Jay
 
dear Jay

I call a function in a COM dll wrote in VB6,that function has a byref parameter whose usage is return value,as VB6 has not the keyword "out"
In VB.NET,can I control the passing-in arguement so that I need not pre-assign a value to the arguement before passing in the function

Regards

Mary
 
Mary,
Put the OutAttribute on the parameter itself.

Imports System.Runtime.InteropServices

Declare Auto Public Function Add2 Lib "mylib.dll" ( _
<[In]> ByVal x As Integer, _
<[In]> ByRef y As Integer, _
<Out> ByRef sum As Integer) As Integer

ByVal are implicitly input, so InAttribute is not needed, however ByRef are
implicitly both input & output, so putting the InAttribute or OutAttribute
on the parameter itself tells P/Invoke that the ByRef parameter is only
being used as input or output.

Using InAttribute & OutAttribute are hints to the P/Inovke marshaller to
help avoid unnecessary copying of data...

For more details search for InAttribute & OutAttribute on MSDN.

Hope this helps
Jay


Mary said:
dear Jay,

I call a function in a COM dll wrote in VB6,that function has a
byref parameter whose usage is return value,as VB6 has not the keyword
"out".
In VB.NET,can I control the passing-in arguement so that I need not
pre-assign a value to the arguement before passing in the function?
 
Dear Jay

Thanks for your response
The approach that you point out is a solution,but it might not suit my work case
My code is server-side program,it accept client-side request,use Type.InvokeMember to call functions in diverse COM dll, according to the method name and parameters provided by client.As to output parameter marked byref in function,client doesn't give the corresponding pass-in arguement,so my code use object-type variable containing null/nothing value to represent it.But it seems it is not allowed to pass null/nothing value to a specific-type parameter in VB.NET
Your approach is in compile time,but my case need run-time solution

I'm looking forward to your suggestion

best regards
Mary.
 
Mary,
Your approach is in compile time,but my case need run-time
solution.
I would not say run-time per se, but I see you point. Its not run-time as I
understand the OutAttribute is interpreted at run-time by the marshaler...
However it is run-time as you need to explicitly declare the variables...

I don't do a lot of late binding (Type.InvokeMember) with COM, So I'm not
sure what to offer.

If you don't have it you may want to purchase Adam Nathan's book ".NET and
COM - The Complete Interoperability Guide" from SAMS Press or one of the
other in-depth .NET & COM interop books...

Also I would the microsoft.public.dotnet.framework.interop to be more
helpful then this group, as the interop group has the developers who do a
lot of interop...

Hope this helps
Jay

Mary said:
Dear Jay,

Thanks for your response.
The approach that you point out is a solution,but it might not suit my work case.
My code is server-side program,it accept client-side request,use
Type.InvokeMember to call functions in diverse COM dll, according to the
method name and parameters provided by client.As to output parameter marked
byref in function,client doesn't give the corresponding pass-in arguement,so
my code use object-type variable containing null/nothing value to represent
it.But it seems it is not allowed to pass null/nothing value to a
specific-type parameter in VB.NET.
 
Doh!
I flipped it, but you get the idea:
I would not say run-time per se, but I see you point. Its not run-time as I
understand the OutAttribute is interpreted at run-time by the marshaler...
However it is run-time as you need to explicitly declare the variables...

Should be "It is run-time as I understand the OutAttribute is interpreted at
run-time by the marshaler, however it is not run-time as you need to
explicitly declare the parameters"...

Jay
 
Dear Jay

I'm sorry for not understanding completely what you say.Now I simply my issue as follow
code snippet in VB6 COM dll like this
----------------------------------------------------------
Public Function MyCalledMethod(ByVal a As double, ByValb As double, ByRef c As String) As doubl
c = "I'm a calculator.
MyCalledMethod = a +
End Functio
---------------------------------------------------------------
The calling code snippet in VB.NET like this
-------------------------------------------------------------
dim A as double=55.
dim B as double=6
dim RetValue as doubl
dim C1 as object="
dim C2 as objec
RetValue =MyCalledMethod(A,B,C1) 'statement
RetValue =MyCalledMethod(A,B,C2) 'statement

--------------------------------------------------------------

Since c in MyCalledMethod is a output parameter, there is no need to pass in a non-nothing value.The statement 1 above can work,but statement 2 can not,as C2 contain the value of nothing.When calling the function,there is no value that can be copy to the paramater C.My question is,is there any approach to prevent copying the value C2 to paramater C
An early reply is appreciated
Regards
Mary
 
Mary,
Obviously you need to take a step back! :-(

Are you using Type.InvokeMethod explicitly as you indicated in your previous
message or are you calling MyCalledMethod directly as you suggest in this
message?

If you are calling MyCalledMethod, forget the fact that behind the scenes
that Type.InvokeMethod maybe called indirectly.

If you are calling the method directly, why don't you have an object
variable in your example?

dim x as Object
x = ' create the VB6 COM object
RetValue = x.MyCalledMethod(A,B,C1) 'statement 1
RetValue =x.MyCalledMethod(A,B,C2) 'statement 2


As it stands now: I do not see that you can do what you are attempting, the
method requires three parameters, so you have to send three parameters, The
third parameter is ByRef, which means you need to give a variable name for
the value. Remember that String in VB.NET can have the value of Nothing, so
I really don't see what you are attempting to state here!

*** IMPORTANT ***
If you don't have it you *really should* purchase Adam Nathan's book ".NET
and COM - The Complete Interoperability Guide" from SAMS Press or one of the
other in-depth .NET & COM interop books...

*** IMPORTANT ***

Hope this helps
Jay

Mary said:
Dear Jay,

I'm sorry for not understanding completely what you say.Now I simply my issue as follow.
code snippet in VB6 COM dll like this:
----------------------------------------------------------
Public Function MyCalledMethod(ByVal a As double, ByValb As double, ByRef c As String) As double
c = "I'm a calculator."
MyCalledMethod = a + b
End Function
----------------------------------------------------------------
The calling code snippet in VB.NET like this:
--------------------------------------------------------------
dim A as double=55.5
dim B as double=66
dim RetValue as double
dim C1 as object=" "
dim C2 as object
RetValue =MyCalledMethod(A,B,C1) 'statement 1
RetValue =MyCalledMethod(A,B,C2) 'statement 2
in a non-nothing value.The statement 1 above can work,but statement 2 can
not,as C2 contain the value of nothing.When calling the function,there is no
value that can be copy to the paramater C.My question is,is there any
approach to prevent copying the value C2 to paramater C?
 
Back
Top