Calling C#-Class from VB.net and Handing over Objects

G

Guest

Hello,

I have a solution with mixed Code (VB.net and C#-Projects).
When I'm trying to build an instance of a C#-Class in a VB.Net-Project there will not be a problem. But if I further try to hand over another class to the instanced C#-Class (which should be done by a property) I 'll be faced with the following translated Error: "C:\...\x.vb(Line No.): A reference to the assembly 'Assembly, containing the class which should be handed over from vb.net to c#', which contains the type ' class ', is necessary. Add one to your project."
But I have already added this reference to both projects (VB.net, C# ) and the versions of the referenced DLLs are the same, so I can't understand my error.
The class which should be handed over is a further VB.Net class.
This Error occurs only form VB.Net to C#.

Who has some help for me.

Thank you in advance.
 
P

Patrick Steele [MVP]

I have a solution with mixed Code (VB.net and C#-Projects).
When I'm trying to build an instance of a C#-Class in a VB.Net-Project there will not be a problem. But if I further try to hand over another class to the instanced C#-Class (which should be done by a property) I 'll be faced with the following translated Error: "C:\...\x.vb(Line No.): A reference to the assembly 'Assembly, containing the class which should be handed over from vb.net to c#', which contains the type ' class ', is necessary. Add one to your project."
But I have already added this reference to both projects (VB.net, C# ) and the versions of the referenced DLLs are the same, so I can't understand my error.
The class which should be handed over is a further VB.Net class.
This Error occurs only form VB.Net to C#.

Not sure about the cause of this exact error, but you might want to
consider defining an interface and place that in an assembly by itself.
Then both projects need only access the interface assembly.

So instead of:

Public Class Dog
Public Sub Bark() ...
Public Sub Walk() ...
End Sub

Define an interface that will be implemented by your class:

Public Interface IDog
Public Sub Bark()
Public Sub Walk()
End Interface

Public Class CoolDog
Implements IDog

Sub Bark() Implements IDog.Bark() ...
Sub Walk() Implements IDog.Walk() ...
End Class

Now your class receiving the reference needs to be only type as an
"IDog" and only needs a reference to the interface assembly.
 
P

Philip Rieck

This is a bug in VS.NET. If you compile this code with the command line,
you will find that there's no problem. I called MS Support on this about a
month after 2002 was released and given a bug #, I've called a few times on
it and been told there isn't a hotfix for it.

You can work around it by using references to the compiled base .dll instead
of using project references (browse for references in the first tab, instead
of using the project reference third tab in the add reference dialog). This
isn't ideal (obviously) but it does work.




Jan Schoenrock said:
Hello,

I have a solution with mixed Code (VB.net and C#-Projects).
When I'm trying to build an instance of a C#-Class in a VB.Net-Project
there will not be a problem. But if I further try to hand over another class
to the instanced C#-Class (which should be done by a property) I 'll be
faced with the following translated Error: "C:\...\x.vb(Line No.): A
reference to the assembly 'Assembly, containing the class which should be
handed over from vb.net to c#', which contains the type ' class ', is
necessary. Add one to your project."
But I have already added this reference to both projects (VB.net, C# )
and the versions of the referenced DLLs are the same, so I can't understand
my error.
 
G

Guest

Hello Philip,

thanks a lot for your help. Your workaround solved my problem quickly. Hopefully Microsoft will give a BUG-Fix to prevent other developpers against the mad torture by finding an error which is not their mistake but a MS Bug...

Greetings and Thanks
Jan
 
G

Guest

Hello Patrick ,

thanks for your help.
Prior to the help of Philip ( posted in this thread) I've tried your solution to solve the mentioned problem. Sorry but I had no success.
I guess that Philip is right.

Thanks and Greetings

Jan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top