1

I have a solution with multiple projects, some of the projects are written in VB, some in C#. I am wondering if there's a way to use interfaces and/or enums written in VB in C# classes? My C# code below doesn't compile, however I am able to see the interface in intellisense.

VB Code:

Namespace A
    Public Inteface IHandler
        Function Handle() As HandlerResult
    End Interface

    Public Enum HandlerResult
        Success = 1
        Fail = 0
    End Enum
End Namespace

C# Code:

using A;
namespace B
{
    public class MyHandler : IHandler
    {
        public HandlerResult Handle(){
            return HandlerResult.Success;
        }
    }
}

P.S It's a console/service application, not ASP.Net (where I know it's doable).

UPD: Sorry guys, was missing a reference to the project with the interface. It's fixed now. I think the thing that in VB projects references are done slightly different than in C# confused me.

3
  • 1
    What's the compilation error? Commented Oct 20, 2011 at 23:36
  • How are VB references different than C# references? Commented Oct 21, 2011 at 15:03
  • @ChrisDunaway, I meant UI for references in visual studio: C# projects have a folder called 'References' with all the referenced libraries, when in VB all the references are done 'behind', in project properties. Commented Oct 21, 2011 at 19:16

2 Answers 2

3

This will work if you put the C# code in a different assembly than the VB code. In order to avoid circular dependencies, you may have to move all of your existing VB code into a DLL assembly. That way you can reference the VB.Net assembly from your new C# assembly, and reference both assemblies from the main exe assembly.

Note that if you do this the namespace when viewed from C# will not be simply "A", it will be nested below the default namespace of the VBProject, e.g.

using StackOverflow7843509.A;

namespace B
{
    public class MyHandler : IHandler
    {
        public HandlerResult Handle()
        {
            return HandlerResult.Success;
        }
    }
}

In this example, the default namespace of the VB project I created was StackOverflow7843509

Sign up to request clarification or add additional context in comments.

1 Comment

It's a different assembly, I added a reference to the right project with the interface and it worked, my bad. Thanks.
0

It all comes down to the same thing when the code is compiled, so the answer is (mostly) yes - you can use compiled VB.NET libraries with C# and vice-versa.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.