tags:

views:

1389

answers:

11

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.

Edit: added "haven't"

+1  A: 

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
+7  A: 

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%5FCode-folder.aspx

brendan
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
+9  A: 

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 MSIL.

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.

Joel B Fant
There are ways to combine two assemblies after the fact, but that wouldn't solve this user's problem.
Steven Sudit
True, but I wouldn't condone it for the sole purpose of mixing languages.
Joel B Fant
I believe you can mix managed and unmanaged c++ in the same project.
Callum Rogers
@C Rogers: That situation is a different critter entirely. You can mix managed and unmanaged C++ in the same source file if you want. But it's still not the same as mixing VB.NET and C#.
Joel B Fant
this isn't strictly the case, but it's how Visual Studio and MSBuild are configured.
Scott Weinstein
A: 

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.

svinto
A: 

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
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
A: 

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
Because you found an error in my post :) We haven't finished converting yet.
Wes P
A: 

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
+1  A: 

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
How does ShartDevelop support this? I just tried it and it does not compile. Is there anything special that needs to be done?
epitka
+1  A: 

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
Dang, that was gonna be my next attempt.
Wes P
A: 

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
A: 

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..)