Microsoft KB Archive/818411

= BUG: You receive a &quot;Reference required to assembly&quot; error message when you run a Visual Basic .NET application =

Article ID: 818411

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



SYMPTOMS
If your Windows Application project refers to both a Visual Basic .NET Class Library project and a Microsoft Visual C# .NET Class Library project, and you call a method in a Visual C# .NET class instance, you receive the following error message:

Reference required to assembly 'ClassLibrary1' containing the type 'ClassLibrary1.Class1'. Add one to your project.

You only receive the error if you have all the following things:
 * A Class Library project in Visual Basic .NET.
 * A Class Library project in Visual C# .NET that refers to the Visual Basic .NET Class Library project.
 * A method in Visual C# .NET that returns an instance of a Visual Basic .NET class.



RESOLUTION
To resolve this bug, refer to the assembly directly instead of adding references to the project. To do this, follow these steps:
 * 1) In Solution Explorer, expand References under WindowsApplication1.
 * 2) Right-click ClassLibrary1, and then click Remove.
 * 3) Right-click WindowsApplication1, and then click Add Reference.
 * 4) Click the .NET tab, and then click Browse.
 * 5) Locate ClassLibrary1.dll in the bin folder of ClassLibrary1.
 * 6) Click ClassLibrary1.dll, and then click Open.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 In Microsoft Visual Studio .NET, start a new class library by using Visual Basic .NET.

By default, Class1.vb is created in ClassLibrary1.  Add the following code to Class1. Public Function display(ByVal i As Integer) As Integer Return i + 20 End Function  On the File menu, point to Add Project, and then click New Project. In Add New Project, click to select Visual C# Projects under Project Types. Under Templates, click to select Class Library.

By default, ClassLibrary2 is created. In Solution Explorer, right-click ClassLibrary2, and then click Add Reference.</li> Click the Projects tab, and then click to select ClassLibrary1.</li> Click Select.</li> In Solution Explorer, right-click Class1.cs, click Rename, and then type Class2.cs .</li> In Solution Explorer, right-click Class2.cs, and then click View Code.</li>  Replace the existing code with the following code: using System; //refer the Visual Basic Class Library using ClassLibrary1; namespace ClassLibrary2 {    public class Class2 {      //the method returns an object of type Class1 public Class1 GetItemObject {                   return new Class1; }          } } </li> On the Build menu, click Build Solution.</li> On the File menu, point to Add Project, and then click New Project.</li> Under Project Types, click to select Visual Basic Projects.</li> Under Templates, click to select Windows Application.

By default, Form1 is created.</li> In Solution Explorer, right-click WindowsApplication1, and then click Add Reference.</li> Click the Projects tab, and then click to select ClassLibrary1 and ClassLibrary2.</li> Click Select.</li>  Add the following code in the Load event of Form1: 'create an instance of the Visual Basic Class Dim vbobj As New ClassLibrary1.Class1 'create an instance of Visual C# Class Dim csobj As New ClassLibrary2.Class2 'call the GetItemObject method of Class2 vbobj = csobj.GetItemObject You may receive the error message mentioned in the &quot;Symptoms&quot; section of this article. </li></ol>

<div class="references_section">