अजपा जप आणि शांभवी मुद्रा ऑनलाईन कोर्स : श्वास, मंत्र, मुद्रा आणि ध्यान यांच्या सहाय्याने मनःशांती, एकाग्रता, चक्र संतुलन आणि कुंडलिनी जागृती. अधिक माहिती आणि आगामी तारखांसाठी येथे जा.


Using C# and VB.NET classes together in the App_Code folder

Introduction

In ASP.NET 2.0 you can mix web forms coded in C# and VB.NET together in a single web site. This works great for web forms. However what if you want to code classes from App_Code folder in different languages? Such mixing of classes coded in different languages is not allowed with default settings. You can, however, configure your web site to get this done. This article is going to tell you how to achieve just that.

Creating sub folders and classes

First of all, create a new web site in VS.NET 2005. Add App_Code folder to it. You can do so easily by right clicking on the web site in the Solution Explorer and choosing "Add ASP.NET Folder" option (see below).

Once you add the App_Code folder add two class files - Class1.cs and Class2.vb. Note that one class file must be in C# where as the other must be in VB.NET. Add the following code in Class1.cs

public class Class1
{
public static string HelloWorld()
{
return "Hello World from C#";
}
}

Similarly, add the following code in Class2.vb.

Public Class Class2
Public Shared Function HelloWorld() As String
Return "Hello World from VB.NET"
End Function
End Class

Both of these classes contain a method called HelloWorld() that simply return a string to the caller.

Now try compiling the web site. What happens? You will get an error as shown below:

Error 1 The files '/AppCodeDiffLang/App_Code/Class2.vb' 
and '/AppCodeDiffLang/App_Code/Class1.cs' use a 
different language, which is not allowed since they 
need to be compiled together.

The error message bluntly tells us that you can not use different coding languages for the classes in App_Code folder. Fortunately, there is a way to get out of this trap. Firstly you need to put C# and VB.NET classes in separate sub-folders under App_Code. Secondly you need to add some markup in the web.config file to tell ASP.NET compiler about your intention.

<codeSubDirectories> Section

Create two folders under App_Code filder named CSCode and VBCode. Move Class1.cs inside CSCode folder and Class2.vb inside VBCode folder.

Add a web.config file to your web site and add the following markup to it:

<compilation debug="true">
<codeSubDirectories>
<add directoryName="CSCode"></add>
<add directoryName="VBCode"></add>
</codeSubDirectories>
</compilation>

Here, we added <compilation> section. The <codeSubDirectories> section defines a set of sub-directories relative to App_Code folder that are compiled at run time. The directoryName attribute points to the sub-folder of App_code. Each sub folder is compiled separately and hence each can have classes coded in different languages. We added our CSCode and VBCode folder in this section. This way the compiler will compile classes from CSCode and VBCode folders separately.

After configuring the web site try to compile it. This time it compiles successfully.

Testing the configuration

Let's create a web form to test our classes. Design the default web form as shown in the following figure.

The web form consists of a RadioButtonList, a Button and a Label. Depending on the selection made the appropriate class is used. The return value of the HelloWorld() method is displayed in a Label.

Add the following code in the Click event of Submit button.

protected void Button1_Click(object sender, EventArgs e)
{
switch (RadioButtonList1.SelectedValue)
{
case "CS":
Label1.Text = Class1.HelloWorld();
break;
case "VB":
Label1.Text = Class2.HelloWorld();
break;
}
}

That's it! You can now run and test the web form.

 

 


Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 21 July 2006