Mixing VB.Net and C# Code in an ASP.Net Web Site project?
Asked Answered
B

3

25

The question quite an older and often asked around, i have similar questions here but my question is a bit more specific.

Q1. Is it legal to mix C# and VB.Net code in ASP.Net Web site? Will it work or not? If it works how it may be done? Any sample will be good.

Q2. If there are any repercussions of mixing C# and VB.Net code then please do share those as well.

I have a web project that is coded in VB.Net. I am working on one module of the project. and i want to code in C#. I cant convert the whole project to C# because i am not the only one working on the project. However, the module i intend to build , i want to have that built in C#.

I have heard that in case of web projects, if we code part in C# and part in VB.net then there are problems compiling the project to a dll. Is that true? if yes then what is the solution.

Also, if i am building a dynamic link library in .Net then can i mix C# and Vb.Net code?

Bryant answered 26/2, 2010 at 6:0 Comment(0)
S
56

From http://msdn.microsoft.com/en-us/library/t990ks23.aspx:

Multiple Programming Languages in the App_Code Folder

Because the source code in the App_Code folder is compiled into a single assembly, all the files in the App_Code folder must be in the same programming language. For example, the App_Code folder cannot include source code in both Visual Basic and C#.

However, you can configure your Web application to treat subfolders of the App_Code folder as separate compilable units. Each folder can then contain source code in a different programming language. The configuration is specified by creating a codeSubDirectories element in the compilation element of the Web.config file and adding a reference to the subfolder. The following example illustrates how you would configure subfolders named VBCode and CSCode to compile into separate assemblies:

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

The references to the VBCode and CSCode subfolders do not need to include any information about what programming language is contained in the subfolder. As with the App_Code folder itself, ASP.NET infers the compiler to use based on the files in the subfolder.

Symbolics answered 26/2, 2010 at 6:17 Comment(1)
The solution given works for a web SITE project. Steve asked about a web APPLICATION project, however, which does not use app_code folders.Rapping
C
7

The answer here that has the most votes only works for Web Site projects, not a Web App project. I think a lot of developers use the term "web application" generically ignoring the fact that technically it is a different thing in .NET. Microsoft says, "By default, a Web application is compiled based on language settings in the project file. Exceptions can be made, but it is relatively difficult." So, I am assuming the only way to do this would be to have two separate projects within one solution. (Again, a Web Site project has no solution or project file.) I'm not sure of all the details.

Counterfactual answered 4/8, 2011 at 15:10 Comment(3)
"(Again, a Web Site project has no solution or project file.)" It can have a solution file, actually (VS may even prompt you to save one), but yes, it has no project.Nosography
@user692918: +1 , Correction done to the title and content. I did have a solution with a web site project with several other projects.Bryant
Is above solution works for Web Application Project? I have large web application with multiple projects. I am going to add new changes on c# with same web project and other Class library projects. Is that works for me?Cephalalgia
P
0

It seems you can mix them within the same project is you do the piece of code you require as a control. So if the main site is in C# and you do the VB part you want as a ascx control, you can drap and drop that within your site.

I did not realise that you could do that, but a team member worked it out on a piece of work he was doing.

Pareto answered 2/11, 2010 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.