168

Can you mix vb and c# files in the same project for a class library? Is there some setting that makes it possible?

I tried and none of the intellisense works quite right, although the background compiler seems to handle it well enough (aside from the fact that I, then, had 2 classes in the same namespace with the same name and it didn't complain).

We're trying to convert from VB to C# but haven't finished converting all the code. I have some new code I need to write, but didn't really want to make a new project just for it.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Wes P
  • 9,622
  • 14
  • 41
  • 48
  • 9
    Why are you writing new code in VB if you just finished converting to C#? – Adam Robinson Aug 14 '09 at 14:10
  • 11
    one of the reasons to have VB code mixed in is to use its unique features, notably its XML capabilities if you're using LINQ to XML for example. That way instead of writing 'from element in root.Elements("Data")' you could use VB's dynamic XML syntax: 'From element In root.'. When working a lot with XML files this syntax can be much easier to work with than C#'s – Marchy Dec 12 '09 at 18:52
  • I have seen your debate and I just did a test:I have a website that has all the pages written in vb.net, code in app_code.. etc. I have just added a new page with code c#, i do stuff in page_load and it works! no error no nothing. – Edd Jun 14 '13 at 09:12
  • @Edd interesting experiment. A different situation than what is being discussed in this question, which is building a class library (.dll) for deployment. But still very interesting. – ToolmakerSteve May 03 '14 at 03:02
  • BTW I see that several of the answers (still being added, years after the question was written) mention solutions for ASP.Net. Those are consistent with the TITLE above - but that is different than a project "for a class library", as the question asks. The accepted answer is correct, for the question being asked. – ToolmakerSteve May 03 '14 at 03:20
  • Incidentally, I thought it might be possible to pull off a hybrid approach using partial classes in two different projects, but partials can't span assemblies, so this won't work, either. – D. Lambert Aug 14 '09 at 14:53
  • @user6808128's solution is correct. Worked for me. https://stackoverflow.com/a/39385842/10412072 – varadharajan Jul 06 '19 at 17:24

17 Answers17

136

No, you can't. An assembly/project (each project compiles to 1 assembly usually) has to be one language. However, you can use multiple assemblies, and each can be coded in a different language because they are all compiled to CIL.

It compiled fine and didn't complain because a VB.NET project will only actually compile the .vb files and a C# project will only actually compile the .cs files. It was ignoring the other ones, therefore you did not receive errors.

Edit: If you add a .vb file to a C# project, select the file in the Solution Explorer panel and then look at the Properties panel, you'll notice that the Build Action is 'Content', not 'Compile'. It is treated as a simple text file and doesn't even get embedded in the compiled assembly as a binary resource.

Edit: With asp.net websites you may add c# web user control to vb.net website

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Joel B Fant
  • 24,406
  • 4
  • 66
  • 67
34

Well, actually I inherited a project some years ago from a colleague who had decided to mix VB and C# webforms within the same project. That worked but is far from fun to maintain.

I decided that new code should be C# classes and to get them to work I had to add a subnode to the compilation part of web.config

        <codeSubDirectories>
            <add directoryName="VB"/>
            <add directoryName="CS"/>
        </codeSubDirectories>

The all VB code goes into a subfolder in the App_Code called VB and the C# code into the CS subfolder. This will produce two .dll files. It works, but code is compiled in the same order as listed in "codeSubDirectories" and therefore i.e Interfaces should be in the VB folder if used in both C# and VB.

I have both a reference to a VB and a C# compiler in

<system.codedom>
    <compilers>

The project is currently updated to framework 3.5 and it still works (but still no fun to maintain..)

Lars Strange
  • 555
  • 4
  • 9
  • See http://stackoverflow.com/questions/15711964/converting-an-asp-net-website-that-has-c-sharp-and-vb-to-a-web-application for more details – John Dec 11 '13 at 08:34
21

You can not mix vb and c# within the same project - if you notice in visual studio the project files are either .vbproj or .csproj. You can within a solution - have 1 proj in vb and 1 in c#.

Looks like according to this you can potentially use them both in a web project in the App_Code directory:

http://pietschsoft.com/post/2006/03/30/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx

brendan
  • 29,308
  • 20
  • 68
  • 109
  • 3
    Downvote because the Project file isn't the reason you can't produce an assembly from them. You can compile an assembly without a Project file ever having existed if you want to, using the compiler from the command line. The reason it won't work is that an assembly is (almost always) one compilation job from one compiler. It's nothing to do with the project file. – Tom W Oct 21 '10 at 14:31
  • 9
    Upvote. From users' point of view the idea that the extension is different is the telltale sign that you can't/. – user4951 Apr 08 '12 at 01:18
15

It might be possible with some custom MSBuild development. The supplied .targets force the projects to be single language - but there's no runtime or tooling restriction preventing this.

Both the VB and CS compilers can output to modules - the CLR's version of .obj files. Using the assembly linker, you could take the modules from the VB and CS code and produce a single assembly.

Not that this would be a trival effort, but it probably would work.

Scott Weinstein
  • 18,890
  • 14
  • 78
  • 115
  • 6
    There is a good write-up on this by Thomas Freudenberg [here](http://thomasfreudenberg.com/blog/archive/2006/08/22/Mixing-C_2300_-and-VB.NET-in-one-assembly.aspx). – Dave M Sep 01 '11 at 18:53
  • 1
    Another example of can be found in [Sample: Mixing Unmanaged C++, C++/CLI, and C# code](http://blogs.msdn.com/b/junfeng/archive/2006/05/20/599434.aspx) by Junfeng Zhang's. It could get it working after first running .\VC\bin\vcvars32.bat from my Visual Studio installation. This is actually mixing different languages is one assembly rather than in one project. – R. Schreurs Jun 02 '15 at 10:36
  • The updated link to Thomas Freudenberg's post here: https://thomasfreudenberg.com/archive/2006/08/21/mixing-c-and-vb-net-in-one-assembly/ – Borislav Ivanov Dec 07 '21 at 16:20
7

Walkthrough: Using Multiple Programming Languages in a Web Site Project http://msdn.microsoft.com/en-us/library/ms366714.aspx

By default, the App_Code folder does not allow multiple programming languages. However, in a Web site project you can modify your folder structure and configuration settings to support multiple programming languages such as Visual Basic and C#. This allows ASP.NET to create multiple assemblies, one for each language. For more information, see Shared Code Folders in ASP.NET Web Projects. Developers commonly include multiple programming languages in Web applications to support multiple development teams that operate independently and prefer different programming languages.

user229044
  • 232,980
  • 40
  • 330
  • 338
gerryLowry
  • 2,626
  • 5
  • 35
  • 44
4

Yes its possible.adding c# and vb.net projects into a single solution.

step1: File->Add->Existing Project

Step2: Project->Add reference->dll or exe of project which u added before.

step3: In vb.net form where u want to use c# forms->import namespace of project.

  • The problem happens when you get too many mixed projects, then you get a circular reference and it won't allow you to add something you need. – Brain2000 Aug 25 '22 at 20:11
2

Although Visual Studio does not support this (you can do some tricks and get MSBuild to compile both, but not from within Visual Studio), SharpDevelop does. You can have both in the same solution (as long as you are running Visual Studio Professional and above), so the easiest solution if you want to keep using Visual Studio is to seperate your VB code into a different project and access it that way.

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • 1
    How does ShartDevelop support this? I just tried it and it does not compile. Is there anything special that needs to be done? – epitka Sep 08 '09 at 20:53
2

Right-click the Project. Choose Add Asp.Net Folder. Under The Folder, create two folders one named VBCodeFiles and the Other CSCodeFiles In Web.Config add a new element under compilation

<compilation debug="true" targetFramework="4.5.1">
      <codeSubDirectories>
        <add directoryName="VBCodeFiles"/>
        <add directoryName="CSCodeFiles"/>
      </codeSubDirectories>
</compilation>

Now, Create an cshtml page. Add a reference to the VBCodeFiles.Namespace.MyClassName using

@using DMH.VBCodeFiles.Utils.RCMHD
@model MyClassname

Where MyClassName is an class object found in the namespace above. now write out the object in razor using a cshtml file.

<p>@Model.FirstName</p>

Please note, the directoryName="CSCodeFiles" is redundant if this is a C# Project and the directoryName="VBCodeFiles" is redundant if this is a VB.Net project.

RickIsWright
  • 531
  • 5
  • 6
2

Why don't you just compile your VB code into a library(.dll).Reference it later from your code and that's it. Managed dlls contain MSIL to which both c# and vb are compiled.

JMax
  • 26,109
  • 12
  • 69
  • 88
Ajov
  • 29
  • 1
  • 3
    You'll be completely stuffed if you ever need to reference any of your new C# code from the legacy VB.NET code. Or vice versa, whichever way you go, it's one-way references only. – Roman Starkov Mar 15 '16 at 22:22
1

I don't see how you can compile a project with the C# compiler (or the VB compiler) and not have it balk at the wrong language for the compiler.

Keep your C# code in a separate project from your VB project. You can include these projects into the same solution.

rein
  • 32,967
  • 23
  • 82
  • 106
1

You need one project per language. I'm quite confident I saw a tool that merged assemblies, if you find that tool you should be good to go. If you need to use both languages in the same class, you should be able to write half of it in say VB.net and then write the rest in C# by inheriting the VB.net class.

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
1

At the risk of echoing every other answer, no, you cannot mix them in the same project.

That aside, if you just finished converting VB to C#, why would you write new code in VB?

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 2
    Because you found an error in my post :) We haven't finished converting yet. – Wes P Aug 14 '09 at 14:56
  • Because there are some language specific things that you might want to be able to use. Such as inline XML. Or unsafe. Or optional refs. – Brain2000 Feb 05 '21 at 18:53
1

For .net 2.0 this works. It DOES compile both in the same project if you create sub directories of in app code with the related language code. As of yet, I am looking for whether this should work in 3.5 or not though.

mgbeers
  • 11
  • 1
0

As others have said, you can't put both in one project. However, if you just have a small piece of C# or VB code that you want to include in a project in the other language, there are automatic conversion tools. They're not perfect, but they do most things pretty well. Also, SharpDevelop has a conversion utility built in.

Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
  • There is that. I could just suck it up and code what I need to in VB and convert it later with the rest of the stuff. – Wes P Aug 14 '09 at 14:57
0

No, not in the same project.but you can use them in the same solution. though you need to take care that your code is CLS compliant. That means you must not have used such functionality/feature that is not understand by other language. For example VB does not understand unsigned ints.

juhi
  • 320
  • 1
  • 3
  • 15
0

In our scenario, its a single VB.NET project (Windows Desktop application) in a single solution. However we wanted to take advantage of C# features like signed/unsigned integers and XML literals and string features in VB.NET. So depending on the features, at runtime we build the code file, compile using respective Rosalyn compiler (VB/CS) into DLL and dynamically load into current assembly. Of course we had to work on delinking, unloading, reloading, naming etc of the dynamic DLLs and the memory management were we largely used dynamic GUID for naming to avoid conflict. It works fine where app user may connect to any DB from our desktop app, write SQL query, convert the connection to LINQ connection and write LINQ queries too which all requires dynamic building of source code, compiling into DLL and attaching to current assembly.

Venkat
  • 1,105
  • 1
  • 8
  • 12
-1

Yes, You can add both of the file in web site only.If the project is a web application it will not allow different type of file.

Python
  • 63
  • 2
  • 10