C# and VB code mixed together in VB.NET

C# and VB.NET code classes can be mixed together in a ASP.NET 2.0. Such mixing of classes in different languages is not allowed with default settings. This article explains how to achieve this.
  • 2919

Introduction

In ASP.NET 2.0 you can use C# and VB.NET web forms together in one web site. But if you want to use classes coded in different languages, Such mixing is not allowed by default settings. However you can configure web site to get this done. This article drill down to achieve this.

Create App_Code and Sub Directories with classes

Create a new web site in VS.NET 2005. Add App_Code folder to it.

img1.gif
 

Add two class files in App_Code- TestClass1.cs(C# code) and TestClass2.vb(VB Code). Build the application. It will show the following error

 

img4.gif
 

To overcome this error add two sub directories(CS,VB) one for C# class and one for VB class

 

img3.gif
 

Now when you try to build the application, it shows the following error

 

ImgUpdate.jpg
 

Here is the trick - If you have class files written in different languages, create language-specific App_Code subdirectories to contain classes for each language. Add entry to the web.config file for each subdirectory. It is important that the language-specific subdirectory is registered in the web.config otherwise, all files under App_Code will be compiled to a single assembly regardless of the folder they belong.

<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="CS"/>
<add directoryName="VB"/>
</codeSubDirectories>
</compilation>

Using multiple languages in the same ASP.NET project is helpful when you're integrating existing code or when you have developers on the project with different skills.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.