Monday, September 10, 2012
C# and VB in the Same Project
You can have C# and VB in the same project if you set up individual directories, with each containing only one language apiece.
Here is a description of how to do that.
Having two classes made up of different languages in the \App_Code folder (as shown here) causes an
error to be thrown. It is impossible for the assigned compiler to work with two different languages.
Therefore, in order to be able to work with multiple languages in your \App_Code folder, you must make
some changes to the folder structure and to the web.config file.
The first step is to add two new subfolders to the \App_Code folder — a \VB folder and a \CS folder. This
gives you the following folder structure:
\App_Code
\VB
Add.vb
\CS
Subtract.cs
This still will not correctly compile these class files into separate assemblies, at least not until you make
some additions to the web.config file. Most likely, you do not have a web.config file in your solution
at this moment, so add one through the Solution Explorer. After it is added, change the <compilation>
node so that it is structured as shown in Listing 1-17.
Listing 1-17: Structuring the web.config file so that classes in the \App_Code folder
can use different languages
<compilation>
<codeSubDirectories>
<add directoryName="VB"></add>
<add directoryName="CS"></add>
</codeSubDirectories>
</compilation>
Now that this is in place in your web.config file, you can work with each of the classes in your ASP.NET
pages. In addition, any C# class placed in the CS folder is now automatically compiled just like any of the
classes placed in the VB folder. Because you can add these directories in the web.config file, you are not
required to name them VB and CS as we did; you can use whatever name tickles your fancy.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment