using c# and vb in the same web project
Can you have both c# and visual basic in the same web site project?
Definitely! It simply wouldn’t make sense from a new project standpoint, code reviews, coding standards, continuity, project maintenance, etc.
however, people still want it. to-date i never really tried (and that’s been my answer). I was presented with a usable scenario of why you may need (not want, need) to do this, so I finally tried it. the answer: yes…kinda…sometimes.
let’s assume we have a web site structure like this:
we have the App_Code folder and a .cs and a .vb file in the same projects (separated into sub-folders). note that the project sees them as folders (yellow folder icon) in the special folder. each class within there basically has a “hello world” function only, like this in the c# file:
public string SayHelloCS()
{
return "Hello from CS";
}
and the visual basic file has a similar function emitting "Hello from VB."
now, if you run default.aspx in this structure, this is what you will see:
The files ‘/WebSite5/App_Code/VBCode/Class2.vb’ and ‘/WebSite5/App_Code/CSCode/Class1.cs’ use a different language, which is not allowed since they need to be compiled together.
interesting? probably not, but it makes sense…so how do we overcome. we use a configuration option called codeSubDirectories. here’s what we need to add to our <compilation> node in our web.config:
<compilation debug="false">
<codeSubDirectories>
<add directoryName="VBCode"/>
<add directoryName="CSCode"/>
</codeSubDirectories>
</compilation>
once we add those codeSubDirectory nodes, let’s “look” at what the project structure looks like now:
as you can see the code folders are now “special” in the eyes of visual studio. now if we browse default.aspx we will see:
Hello world from CS. Hello world from VB
Leave a Reply
You must be logged in to post a comment.