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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Schoenrock

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

    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(Li ne 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.
  • Patrick Steele [MVP]

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

    In article <40F0233E-EADF-41D2-B43F-F6F7D084A373@mi crosoft.com>,
    Jan.Schoenrock@ Hamburg.de says...[color=blue]
    > 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(Li ne 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#.[/color]

    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.

    --
    Patrick Steele
    Microsoft .NET MVP

    Comment

    • Philip Rieck

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

      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" <Jan.Schoenrock @Hamburg.de> wrote in message
      news:40F0233E-EADF-41D2-B43F-F6F7D084A373@mi crosoft.com...[color=blue]
      > 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[/color]
      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(Li ne 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."[color=blue]
      > But I have already added this reference to both projects (VB.net, C# )[/color]
      and the versions of the referenced DLLs are the same, so I can't understand
      my error.[color=blue]
      > 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.[/color]


      Comment

      • Jan Schoenrock

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

        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

        Comment

        • Jan Schoenrock

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

          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


          Comment

          Working...