473,378 Members | 1,469 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,378 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 34057
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 ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.