Monday, October 27, 2008

Use C# and VB.NET in the same project

Use C# and VB.NET in the same project
Use C# and VB.NET classes in the same ASP.NET application


Introduction

Whilst it isn't ideal to both mix C# and VB.NET languages in the same ASP.NET project, it is possible. The code still has to be separated and it can't be mixed at page level but you can create a class in each language and then use and reference them both from any page.

Main

Step 1

Create an App_Code folder if it doesn't already exist. Then, inside this folder create two other folders to hold each language type.

For example,

App_Code
vbcode
cscode

Step 2

Edit your web.config file and make sure the following section is added:

<SYSTEM.WEB> 

<COMPILATION>
<CODESUBDIRECTORIES>
<ADD directoryName="vbcode" />
<ADD directoryName="cscode" />
CODESUBDIRECTORIES>
COMPILATION>
SYSTEM.WEB>

Add each class to the relevant folder and you're done! You can now reference any of the classes in the standard manner and ASP.NET will do all of the compilation automatically for you.
Conclusion

Certain scenario's may exist where it's required to use .NET code that was written in two different languages. Fortunately, it is fairly easy to implement to implement this in your project by specifying the two languages in your web.config file and telling ASP.NET which code directories to use.

No comments: