Wednesday, March 28, 2007

C# and VB together?

I have an app in VS 2005 that contains both VB and C# code in the same website project. At compile time I kept getting this error:

"The files '/WebSite/App_Code/VB/test.vb' and '/WebSite/App_Code/CS/test.cs' use a different language, which is not allowed since they need to be compiled together."

To go around this issue, simply add the following to your web.config file under the compilation tag:

&ltcodeSubDirectories>
&ltadd directoryName="CS">
&ltadd directoryName="VB">


In this case, all the VB files and C# files were already separated into different folders inside the App_Code Folder.

Hope this helps.

2 comments:

Bhanu Chintha said...

Hi Diego,

I am using .NET 2.0 and i am not getting any error when i have *.cs and *.vb files in one project. Even i did did not add any section inside the compilation section for codeSubDirectories.

Also, i have one more doubt, what about the class library projects that doesn't have any config file.

Thanks & Regards,
Bhanu

Unknown said...

Hi Bhanu,

I am not sure how you are not getting a compilation error mixing up 2 different languages into 1 assembly. What type of project is it? a website? a web application? a class library? When you create a project, the first thing you have to select is the language. After doing that, you select the project type, let's say a Console Application. The whole project or assembly will use the compiler of the language you selected. If you add a new item to that project, then all of the options you will have will in the language you originally selected. Even if you try to change the extension of the file, once the file is created, you will have no intellisense, because the compiler will not recognize that file extension. Now, just to be clear, I am not talking about a Solution. In Solutions you absolutely can have different languages mixed in without any additional configuration. Each project will use the compiler you selected and build the dlls which then will be put into a bin directory. Once they are in compiled into a .dll, then the language doesn't matter since its already IL (intermediate language) and that's all the CLR cares about. The post explains how to have C# and VB in the same "website" project. I am not sure this can be done on any other project type. Not even a Web Application Project since the "App_Code" folder does not exist.

As far as class libraries, again, I don't think this is possible, but you might be able to give it a whirl using the app.config file.

If you are able to mix these 2 languages into a single project, I would love to know how you do it. Can you post some example code?

Thanks

Diego