简体   繁体   中英

Type Is not defined - C# class to VB.NET

I am sitting with a rather strange error - which I believe I followed the correct steps to solve.

Basically, My compiler complains about the following:

Type 'InteractionsEntities' is not defined

Here is an explanation of all I have done. My goal is to use a class compiled in C# within my vb.net project.

Step 1 : Right clicked my VB.NET project - Add > Reference

Step 2 : Out of the project list, I selected my C# project containing the InteractionsEntities class and pressed Ok.

Step 3 : I added the line of code in my vb.net module: Imports CsharpProject.CsharpNamespace

Step 4 : Within my module, I added a variable: Private context = new InteractionsEntities -- note on this point, Intellisense was able to find the class I required.

Step 5 : To ensure my Project can use entity framework, I used the nuget package manager to install entity framework.

So following the steps I listed above, I have the following code:

Imports CsharpProject.CsharpNamespace

Module Module1
    Dim context = New InteractionsEntities()
    Sub Main(properties As String())
            Dim documents = context.Documents.Select(Function(x) x)
            For Each document In documents
                Console.WriteLine(document.Name)
            Next
    End Sub

What is strange is that Intellisense was able to detect that InteractionsEntities existed in my C# namespace. Upon Installing the entityframework nuget package, the error would disappear - and I was able to access properties within the context variable. The moment I hit "rebuild all" - the error Type 'InteractionsEntities' is not defined returns. Hovering over the namespace (which also gets marked as erroneous now) and clicking the Error Corrections Options yields no Correction Suggestions.

Have I missed a step inbetween? Why is my VB.NET project complaining that the class does not exist when it does? I have tested with other classes under the c# namespace too (which has nothing to do with the entity framework) and the same effect occurs.

Wow ok. I found the problem.

When this happens, ensure that your C# project and VB.NET project are at least compiled in the same .NET Framework version.

My inspection in my project was the following:

  1. Right-click C# Project > Properties > Application > Look at target Framework (my case it was 4.5.1)
  2. Repeat the steps for the VB.NET Project > Properties > Application > Look at target Framework (my case it was 4.5)
  3. Set the .NET Framework of the VB.NET Project to match that of the C# Project and click yes in the dialog box which pops up.
  4. Rebuild project

This fixed my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM