M
Michi Henning
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.
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.