Sunday 20 January 2008

Mix n match C# and VB codes in a single project

Got this wonderful stuff from http://www.aspnetlibrary.com

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.

To do this, you'll need to follow a few simple steps.

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>

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

No comments: