473,224 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Output Parameters in VB .NET ?

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

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com
Nov 20 '05 #1
10 33998
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]
"Juan Sutton" <jc******@usa.com> wrote in message
news:23**************************@posting.google.c om...

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

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com

Nov 20 '05 #2
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.

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message news:<#e**************@TK2MSFTNGP11.phx.gbl>...
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]
"Juan Sutton" <jc******@usa.com> wrote in message
news:23**************************@posting.google.c om...

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

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com

Nov 20 '05 #3
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

"Juan Sutton" <jc******@usa.com> wrote in message
news:23**************************@posting.google.c om...
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.

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com

"Rob Teixeira [MVP]" <RobTeixeira@@msn.com> wrote in message

news:<#e**************@TK2MSFTNGP11.phx.gbl>...
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]
"Juan Sutton" <jc******@usa.com> wrote in message
news:23**************************@posting.google.c om...

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

-----------------------------------------------------
The winds of life blow on each of us.
Is your sail set in the right direction?
http://www.ICreateMyLife.com

Nov 20 '05 #4
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
Nov 20 '05 #5
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" <an*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
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

Nov 20 '05 #6
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.
Nov 20 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com... 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.


Nov 20 '05 #8
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl... 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" <an*******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
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.


Nov 20 '05 #9
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

Nov 20 '05 #10
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" <an*******@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com... 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
---------------------------------------------------------------

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.

Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bari Allen | last post by:
I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First varchar(80) OUTPUT ,...
5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
4
by: Janaka | last post by:
Hi this is kind of an ASP.NET/ADO.NET question. I've got a stored procedure on SQL Server that returns a results set. It also sets 3 output parameters in a seperate Select statement. When...
2
by: Bari Allen | last post by:
ASP Classic question: I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First...
3
by: juststarter | last post by:
Hello all, I am executing a stored procedure (on an SQL Server) using ODBC but i can't get the output parameter's value on the client. The stored proc has 3 parameters ,2 of them are input and 1...
1
by: Joe Van Meer | last post by:
Hi all, I have an app that currently runs through 3 seperate stored procedures each returning a count of records. What I would like to do is combine these calls into one call, however I am...
1
by: Garth Wells | last post by:
Using an example in the Jan 2006 release of the Enterprise Library, I came up with the code shown below to create a DAL method for returning several columns of a single row. I place the output...
6
by: c676228 | last post by:
Hi everyone, I wrote a store procedure that fetch one row data in the database based on the parameter value I entered. After I created the store procedure, the store procedure code looks like...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.