473,322 Members | 1,409 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,322 software developers and data experts.

Out parameters, VB and C#

Hi,

I'm generating both VB and C# code from language-independent interface
definitions, which is why I'm raising this issue. (The problem apppears
to be somewhat esoteric, but it is real in my situation and is about
more than just convenience.)

C# has the notion of an out parameter (which is different
from pass by value and pass by reference), but VB only understands
pass by value and pass by reference. This means that parameters that
notionally are out parameters end up as ByRef parameters in VB.
I would like to find a way to have VB source code produce parameters
that look like out parameters to C#.

Here is a very simple example. Suppose I have the following in VB:

' VB:
Public Sub setToFortyTwo(ByRef i As Integer)
i = 42
End Sub

I bundle this method into an assembly that I call from C#.
As it stands, I can call the method as:

// C#:
int i = 0; // Must initialize, because call is by reference
setToFortyTwo(ref i);

What I would like to do instead is the following:

// C#:
int i; // No need to initialize for out parameters
setToFortyTwo(out i);

Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?

I had a look through the language reference, but I can't find
anything applicable. But, possibly, there is some trickery with
MarshalAs that could be used? Basically what I need is a way to
tell the VB compiler that "this is *really* an out parameter and
I want you to mark it as such in the assembly."

I'd even consider post-processing the code that is generated by VB
to change the parameter type, assuming that there is some documentation
around that explains how the metadata is stored in an assembly or
an object file. Can someone point me in the right direction for this,
failing an easier solution?

Thanks,

Michi.
Nov 16 '05 #1
9 1701
Michi Henning <mi***@zeroc.com> wrote:

<snip>
Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?


I *think* that you can use OutAttribute.

Import System.Runtime.InteropServices

and then use <Out> on the parameter declaration.

It seems to work from a quick test, but you'll obviously want to look
at it a bit more carefully.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jon Skeet [C# MVP] wrote:
Michi Henning <mi***@zeroc.com> wrote:

<snip>
Is there some attribute I can use to instruct VB to mark the
parameter as an out parameter in the generated Assembly?

I *think* that you can use OutAttribute.

Import System.Runtime.InteropServices

and then use <Out> on the parameter declaration.

It seems to work from a quick test, but you'll obviously want to look
at it a bit more carefully.


Thanks muchly for that! After much browsing through the doc, I just found
this myself too. Works like a charm:

' VB:
Public Sub setToFortyTwo(<System.Runtime.InteropServices.out( )> ByRef i As Integer)
i = 42
End Sub

This does the trick very nicely.

Thanks again for your help, and I apologize for having shouted for help
just that little bit too soon :-|

Cheers,

Michi.
Nov 16 '05 #3
Michi Henning <mi***@zeroc.com> wrote:
Thanks muchly for that! After much browsing through the doc, I just found
this myself too. Works like a charm:

' VB:
Public Sub setToFortyTwo(<System.Runtime.InteropServices.out( )> ByRef i As Integer)
i = 42
End Sub

This does the trick very nicely.
Excellent. I'd personally use an import and just have the parameter as
(<Out> ByRef i as Integer)
but that's a different story :)
Thanks again for your help, and I apologize for having shouted for help
just that little bit too soon :-|


No problem at all - it's not entirely obvious from the docs. It should
really be mentioned clearly in the ByVal vs ByRef documentation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon,
It should really be mentioned clearly in the ByVal vs ByRef documentation.


Do you mean in the VB docs? I don't see why VB (or any other language
for that matter) documentation should have to mention C#-isms such as
out parameters. Instead, the C# docs for out should mention that it's
really just a shorthand for [Out] ref.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #5
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michi Henning <mi***@zeroc.com> wrote:
Thanks muchly for that! After much browsing through the doc, I just found
this myself too. Works like a charm:

' VB:
Public Sub setToFortyTwo(<System.Runtime.InteropServices.out( )> ByRef i As Integer) i = 42
End Sub

This does the trick very nicely.


Excellent. I'd personally use an import and just have the parameter as
(<Out> ByRef i as Integer)
but that's a different story :)


Yes, normally, I would do just that. But, in this case, this is code generated
by a compiler, and I have to be careful about name clashes between the
generated code and user-defined symbols, so I can't import an entire
namespace wholesale, for fear of symbol clashes.
Thanks again for your help, and I apologize for having shouted for help
just that little bit too soon :-|


No problem at all - it's not entirely obvious from the docs. It should
really be mentioned clearly in the ByVal vs ByRef documentation.


Yes, would be nice, wouldn't it?

Cheers,

Michi.

--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com

Nov 16 '05 #6
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Jon,
It should really be mentioned clearly in the ByVal vs ByRef documentation.


Do you mean in the VB docs? I don't see why VB (or any other language
for that matter) documentation should have to mention C#-isms such as
out parameters. Instead, the C# docs for out should mention that it's
really just a shorthand for [Out] ref.


No, this clearly belongs with VB. For example, VB may need to call into
a DLL written in C# that uses out parameters. To some people, it may
not be obvious how to pass the parameter. Or, alternatively, I may
need to provide a callback function written in VB to a DLL written
in C#. If that callback function uses out parameters, I need to know
how to create an out parameter in VB.

Cheers,

Michi.

--
Michi Henning Ph: +61 4 1118-2700
ZeroC, Inc. http://www.zeroc.com

Nov 16 '05 #7
For example, VB may need to call into
a DLL written in C# that uses out parameters.
They are seen as regular ByRef parameters from VB. The distinction
between out and ref when calling such a method doesn't matter, since
locals are always initialized in VB (so C# definite assignment rules
don't apply).

Or, alternatively, I may
need to provide a callback function written in VB to a DLL written
in C#. If that callback function uses out parameters, I need to know
how to create an out parameter in VB.


Not really. Again, you can just treat it as a regular ByRef.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #8
Mattias Sjögren <ma********************@mvps.org> wrote:
It should really be mentioned clearly in the ByVal vs ByRef documentation.


Do you mean in the VB docs? I don't see why VB (or any other language
for that matter) documentation should have to mention C#-isms such as
out parameters. Instead, the C# docs for out should mention that it's
really just a shorthand for [Out] ref.


I think it should probably be mentioned in both C# and VB.NET
documentation, to be honest. (And it's not in the C# spec as far as I
can see, unfortunately.)

Given that it's something which can affect more languages than just C#
though, I don't see why it shouldn't be mentioned briefly in the VB.NET
docs though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
I don't see why you should be bothered when you know the usage of the
out parameter in C#, simply use it in a c# class and have the method
called through another method in the same class. This way you will have
the value in an out variable in the C# class. Have the value of the out
variable returned to the VB.Net object.
with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

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

Similar topics

7
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
by: John Kotuby | last post by:
Hi all, I am working on porting an application from VB6 to VB.NET 2003 and am running into some problems. When declaring and populating the parameters for a SQL Stored Procedure by using the...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
2
by: Jared Grant | last post by:
I am trying to find the value from some output parameters from a stored procedure. I have tried several different methods but somehow cannot get it to work. here is my source code: dim dr as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.