Cry about... .NET How To ...
How to mix C# and VB.Net in the same project
It is possible to mix C# and VB.Net in the same project, but only if it is a web-project.
Normally when a new project is created it is created as a C# project or as a VB.Net project.
Class Libraries (cannot mix)
A class library cannot contain both C# and VB.Net. From C# you can use classes and methods written in VB.Net and vice-versa but only if these are provided by other class libraries.
If you need to develop with both C# and VB.Net then the best you can do is to create separate class libraries for each.
Desktop Application (cannot mix)
When developing a desktop application, C# and VB.Net cannot be mixed. The project can use class libraries which are written in C# or VB.Net (although each class library cannot be mixed internally), but the project itself must be C# or VB.Net and the two cannot be mixed.
Web Application (cannot mix)
When developing a Web Application (not a "Web Site"), VB.NET and C# cannot be mixed. However you can use class libraries and those can be either C# or VB.NET.
Web Site Project (can mix C# and VB.Net)
It is possible to mix C# and VB.Net in a web site. (Note, "Web Site" not "Web Application".)
Web Forms can be either
A web-project created for C# will default to C# when adding new web forms or to VB.Net if the project was created as a VB.Net project. However when adding a new form you can select whether the new form is a "Visual Basic" Web Form, or a "Visual C#" web form.
So you could for example create a C# web form in a VB.Net project, and link it to be a VB.Net master page.
This has been tried and tested with Visual Studio 2010 and Visual Studio 2008. I am dubious whether it works for earlier versions of Visual Studio.
App_Code
Within the App_Code
folder you cannot mix C# and VB.Net - at least
not in the same folder and even then not without tweaking the web.config
file.
It appears that (out of the box) the Visual Studio compiler requires all the files in
the App_Code
folder and in sub-folders to be in the same language. So it
would work to have a VB.Net project and have ALL of the files in the
App_Code
folder C#.
If you want to mix both C# and VB.Net then you need to:
- Create a separate folder in the
App_Code
for your VB.Net code (or for your C# code). If your project is a C# project then technically you need only create a separate folder underApp_Code
for your VB.Net code, but it is probably clear to create one subfolder for your C# code and a second one for your VB.Net code. - Modify your
Web.config
file to add a<codeSubDirectories>
element. This element lets you list directories which are to be compiled separately. The<codeSubDirectories>
element nests within the<compilation>
element, which in turn is inside the<system.web>
element which is nested inside the<configuration>
element.
So, lets say that you create the folders "CS_Code" and "VB_Code"
under App_Code, the first for all your C# code and the second for your
VB code. In your Web.config
file you would then have (as a
minimum):
<compilation debug="false">
<codeSubDirectories>
<add
directoryName="CS_Code"/>
<add directoryName="VB_Code"/>
</codeSubDirectories>
</compilation>
The <codeSubDirectories>
element was introduced with .NET 2, but I have
not tested this with versions of Visual Studio prior to 2008.
These notes have been tested within Microsoft Visual Studio 2012, 2010 and 2008, and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.