473,382 Members | 1,750 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,382 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 21 '05 #1
8 19705
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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '05 #9

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

Similar topics

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...
1
by: Aruna Dalvi | last post by:
Hi friend Please tell me why this error is occured ERROR Too few parameters. Expected 22. Cod Dim InsertString As String = "INSERT INTO TableHR1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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?
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.