Use VB.NET and C# in the same application?

48,087

Solution 1

I've never done it myself, but I know you can compile the C# code into a dll and then load and reference the dll in your VB project.

From "Calling C# class in VB.net":

I think the C# code that you want to use must be compiled as a DLL. Once that is done, simple add a reference to that project to your VB.Net project, import the namespace you need, and then you can use the C# code.

Also see How To: Create and Use C# DLLs (from MSDN, for VS2005)

Solution 2

I just want to know that is it possible to use both VB and C# in the same project.

No, not in the same project. On the other hand, you can use them in the same solution.

Or can i include a module written in C# in my VB.net project.

I propose that you create a solution containing two projects: one in C# which forms a library that you use from your VB project. This is straightforward, easy to maintain and easy to extend.

Solution 3

You also want to ensure that you C# code is CLS compliant. This means that it won't publicly expose any functionality which other .NET languages won't understand (for example unsigned ints - which don't exist in VB, or differing classes only by case - since VB is not case-sensitive). To do this you need to add an attribute so that the compiler will raise errors if you have broken any of the guidelines. This article shows you how to do this:

The CLSCompliantAttribute can be applied to assemblies, modules, types, and members.

For marking an entire assembly as CLS compliant the following syntax is used

using System; 
[assembly:CLSCompliant(true)]

For marking a particular method as CLS compliant the following syntax is used

[CLSCompliant(true)]  
public void MyMethod()`

Solution 4

You can't use a C# file and VB file in the same project. You can, however, have VB and C# projects in the same solution and reference them.

In your code you can use:

Imports namespace 

or

using namespace

Once the reference has been added to the appropriate project build the solution and you are good to go.

You can also create a VB.NET Library in a separate solution, compile it and import the DLL into the C# Project or vice versa.

Solution 5

Put VB.NET and C# code in separate projects. (I am using both VB.NET and C# in my open source project, http://msquant.sourceforge.net/, and it works great).

You don't need to worry about DLLs, just reference the project (use tab "Project" in the "Add Reference" dialog box). E.g. if you need to use a function in the C# code/project add a reference in the VB.NET project to the C# project.

Share:
48,087
Ming Liu
Author by

Ming Liu

Updated on October 25, 2020

Comments

  • Ming Liu
    Ming Liu over 3 years

    I am developing a GUI based application in MS Visual Studio 2005, I just want to know if it is possible to use both VB.NET and C# in the same project. Or can I include a module written in C# in my VB.NET project?

    I have a class written in C# which I want to use in my VB.NET based project, so if I can include and call functions from that project than I won't have to write the class again in VB.NET.

    So please help me as I am new to .NET programming.

  • Ming Liu
    Ming Liu about 15 years
    thank you bery much for such prompt reply... I think what you have told me is the same as Jared but easier to do, so i'll go with Jared's solution...
  • Martin Harris
    Martin Harris about 15 years
    Ah, my mistake - I'm not a VB programmer. They still aren't CLS compliant though so the error would still be flagged even though I guess you could ignore it in this specific case.