passing structs from VB to C# and back to VB

  • Thread starter Thread starter Andreas =?ISO-8859-1?Q?M=FCller?=
  • Start date Start date
A

Andreas =?ISO-8859-1?Q?M=FCller?=

Hi All,

I have the following situation:

Assembly VB defines a structure in VB.NET:
Public Structure TP
Public i As Integer
End Structure

Assembly CS (in C#) uses this structure and returns it from a function
CONV.exec() ). Additionally it defines a second test struct and a second
function to return the test struct:

public struct TST
{
public int i;
}
public class CONV
{
public static VB.TP exec()
{
VB.TP tp =new VB.TP();
tp.i=42;
return tp;
}
public static TST exec2()
{
TST tp =new TST();
tp.i=42;
return tp;
}
}
The assembly has a reference to assembly VB

Now the assembly VBCA (writen in VB) uses the C# assembly. It references
the VB and the C# assembly:

Sub Main()
Dim tst1 As VB.TP = CS.CONV.exec()'doesn't work, struct defined in VB
Dim tst2 As CS.TST = CS.CONV.exec2() 'works, struct defined in C#
End Sub

The compile of the first statement fails, when the Type is defined in a
VB assembly:
error BC30652: Reference required to assembly 'VB' containing the type
'VB.TP'. Add one to your project.

However, the second statement that uses a C# defined struct that is
absolutely identical to the VB one compiles and works.

Any ideas what is the culprit?

Thanks in advance,
Andy
 
What are you using to build and compile? IT sounds like you are missing a
reference on the compile line.
With the .NET Framework v1.1, I biult this successfully.
Source code follows:

============ makefile =============
..SUFFIXES: .tlb .il .dll .cpp .cs .vb .rc .ocx

# V1.1
_NETHOME=$(WINDIR)\Microsoft.NET\Framework\v1.1.4322
_NETSDK=C:\netsdk

_DBG_FLAGS=/debug+
_DLL_FLAGS=/t:library $(_CS_DBG_FLAGS)
_EXE_FLAGS=/t:exe $(_CS_DBG_FLAGS)

_CSC=$(_NETHOME)\csc.exe
_VBC=$(_NETHOME)\vbc.exe


test1.exe: test1.vb s1.dll s2.dll
$(_VBC) $(_EXE_FLAGS) /R:System.dll /R:s1.dll /r:s2.dll /out:$@ test1.vb

s1.dll:
$(_VBC) $(_DLL_FLAGS) /out:s1.dll s1.vb

s2.dll:
$(_CSC) $(_DLL_FLAGS) /R:s1.dll /out:s2.dll s2.cs

clean:
-del *.dll

============ s1.vb =============

Namespace VB

Public Structure TP
Public i As Integer
End Structure

End Namespace

============= s2.cs ==================
namespace CS {

public struct TST
{
public int i;
}

public class CONV
{
public static VB.TP exec()
{
VB.TP tp =new VB.TP();
tp.i=42;
return tp;
}
public static TST exec2()
{
TST tp =new TST();
tp.i=42;
return tp;
}
}
}
=============== test1.vb ==================

Imports System

Public Class VbTest1

Public Shared Sub Main()
Dim tst1 As VB.TP = CS.CONV.exec() 'works fine, the type is defined
in VB
Dim tst2 As CS.TST = CS.CONV.exec2() 'works fine, struct defined in C#
End Sub

End Class

-Dino
Microsoft
 
Hi,

I'm using a Solution containing all three projects in Visual Studio
2003. References are set as Project References in this order:

CS -> VB
VBCA -> VB & CS

As you see, the reference in the VBCA project to the VB project that
defines the struct is there, however, it refuses to compile in the IDE.
The commandline approach below works, because it references the compiled
assembly directly. If I setup a direct link to the compiled "VB"
assembly in the IDE, it will work, however, this destroys the single
Solution approach. Additionally, why can't the IDE handle a direkt
reference and not a project reference in this situation?

Cheers,
Andy
 
In the VB Project, check the root namespace. (right click the
Project...Properties, then look under the Common Properties, General )
When I define a new VB project, I get a default root namespace.
 
The namespace is the project name, as expected.
If I use a statement like this in VBCA:

Dim tst1 As VB.TP

it will compile, so it dies find the correct reference here.

However, adding
tst1 = CS.CONV.exec()

will fail.
 
Back
Top