2

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.

Eon
  • 3,833
  • 10
  • 46
  • 75
  • Is the error reproduced if you Clean your solution, then manually build your C# project first, and VB project after? – Justin Ryan May 12 '15 at 10:00
  • That's an idea. Let me clean and rebuild the solutions in order you listed – Eon May 12 '15 at 10:03
  • Sadly the error still occurs – Eon May 12 '15 at 10:03
  • That also didn't help @SébastienSevrin. It is a valid Idea you're posting there, and trust me - I have tried it (and went less and less fancy with each try trying to find the root cause of the problem). I managed to solve the issue by checking which .NET framework each project compiles to - which mismatched in my case. – Eon May 12 '15 at 10:14

1 Answers1

12

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.

Eon
  • 3,833
  • 10
  • 46
  • 75
  • 1
    Interesting. According to [this](http://stackoverflow.com/questions/2433108/can-you-mix-net-framework-versions-in-a-solution), it should be a problem. – Justin Ryan May 12 '15 at 10:17
  • 2
    I only wonder why they haven't added warnings or somesuch when you add a reference to your project with a .NET Framework version which is higher than your current. Instead it gave me a detour by giving me different compile time errors which had nothing to do with what the actual issue was – Eon May 12 '15 at 10:21
  • I believe that if your VB project was targeting a later version it would probably work as well. Good job finding the answer yourself, though you might want to generalize the question a bit. – Zohar Peled May 12 '15 at 10:22