Tech Off Thread

24 posts

Mix VB.NET & C# not possible?

Back to Forum: Tech Off
  • User profile image
    dotnetjunkie

    I thought that in VS2005 you could have .cs and .vb files in the same project?

    I just tried with an asp.net project and it doesn't work!
    It says that the two files use a different language, which is not allowed.

  • User profile image
    AndyC

    You can mix C# and VB projects in the same solution, but I don't think you can mix them within a project.

  • User profile image
    sgomez

    You can't have C# and VB.Net files within the same project.

    seb
    http://sgomez.blogspot.com

  • User profile image
    Dan

    You can't have App_Code files with both VB and C#, but you can have one VB ASP.NET page and one C# ASP.NET page in the same project.

    Thanks,
    -Dan

  • User profile image
    dotnetjunkie

    Dan: So it's not possible to add a normal C# class to a VB.NET web project?  A page is a class as well, so that's kind of strange.

  • User profile image
    reinux

    I think it's because ASPX pages are compiled on the fly, whereas executables and libraries are compiled by their corresponding compilers, csc.exe in C#'s case.

    I guess technically someone could write a compiler that puts VB and C# together, but that'd defeat .NET's goal of being language independent, since that compiler would have to accomodate for every language out there.

    There's more than 50 .NET languages out there right now, and ASPX supports only two of them.

    It's just modularity on a different level, ASPX being arguably more practical but definitely less holistic.

  • User profile image
    dotnetjunkie

    reinux: I was not talking about executables or libraries but a simple class file or webservice in a web project!

    The files in your App_Code directory are compiled on the fly as well, just like the pages, so it must be an architectural limitation!

    It seems that everything in App_Code is compiled together in one assembly???!!

    Can someone from the team tell a bit more about the exact limitations / architecture and possible solutions?

  • User profile image
    reinux

    dotnetjunkie wrote:
    reinux: I was not talking about executables or libraries but a simple class file or webservice in a web project!

    The files in your App_Code directory are compiled on the fly as well, just like the pages, so it must be an architectural limitation!

    It seems that everything in App_Code is compiled together in one assembly???!!

    Can someone from the team tell a bit more about the exact limitations / architecture and possible solutions?

    Ahh sorry, I misread.

    Weird... I always thought you could do it.

    I can see it being a limitation of CodeDom's architecture though, since CodeDom can spit out assemblies but it can't spit out just raw objects.

    There'd be ways to get around it, I'm sure, like ILMerge does, but I guess they figured it wouldn't be worth the trouble Sad

  • User profile image
    JohnAskew

    Maybe compile one as a dll with a nice Interface(s) and use it in the other project through a reference?

  • User profile image
    Sven Groot

    dotnetjunkie wrote:
    It seems that everything in App_Code is compiled together in one assembly???!!

    That is correct. This assembly is referenced when compiling the pages.

  • User profile image
    dotnetjunkie

    Thanks, that explains why it can't be done.
    I hope that people from the team are reading this, as I think this would be a nice improvement for vNext!

  • User profile image
    CRPietschma​nn
  • User profile image
    kquad

    I am having the same type problem, but with a non web project. I would like to not have to rewrite the C# code, but want to use it in my VB.net project.  Any suggestions on if this is possible?

     

    Kquad

  • User profile image
    figuerres

    why not just build a dll and use it??

    simple to do, works everytime.


    no magic, just build a class-lib project and then ref-the dll in another project.

  • User profile image
    Andrew Webber FX

    csc.exe /t:module and al.exe are your friends Big Smile

  • User profile image
    Ion Todirel

    dotnetjunkie wrote:
    I thought that in VS2005 you could have .cs and .vb files in the same project?
    you can have files in different languages in the same project, if you are using C++/CLI, at least in Beta 1, 2 you could.

  • User profile image
    aftab

    We can mix up both C# and VB.Net in App_Code. For that purpose we have to perform these two steps:
    1. Create two subdirectories in App_Code folder one each for C# and VB.Net. Let suppose i have created directory named "VBCode" for VB.Net and "CSCode" for C#.
    2. Modify the complition section of web.config file like this

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

  • User profile image
    odujosh

    You can also make a class lib project (compiles to dll) and put the respective code in their own projects in the bin directory of website. I highley recommend this approach. I am not sure I see the benifit of using app_code except in really trivial cases or pages like global.ASAX. I would avoid it. dlls make utility code more distributable. (of course I do not condone dll spam, making yet another dll)

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.