Possible to combine C#.net, VB.net and ASP.net? - c#

I want to build a website in ASP.net, and to integrate a module which is written in C#(too complicated to rewrite in VB or ASP).
Now I just want to know whether it is possible to have a website that integrates all three?
Thank you.

Yes. You can have a WebSite integrating all three.
Just make your complicated C# a .NET library and include it in your VB.NET/ASP.NET project.

I think you only have to reference the dll's from your modules which are written in c# or VB to call your public functions. I've done this in a windows form application and it worked fine.

You may also want to consider simply learning C#. Most of what you know from VB.NET is directly usable in C#, since most work involves using framework classes and the languages share most features.
It is mostly a matter of syntactic preference, and while a new syntax may sting your eyes for a bit the learning curve will be steep and soon you will have left your old preferences behind.

You have a couple of options when integrating languages in ASP.NET. If you're creating a "web site" (as opposed to a "web application" which can use only one language) then each page can use a specified language, because essentially each page is compiled separately. Some can use VB, some can use C#. No problem.
However, what you describe sounds even simpler. You claim that there is an existing C# module which you want to use in your VB website. Is this module compiled into its own assembly? If that's the case then the language that was used to create the assembly is immaterial. Once it's compiled, it's a .NET assembly and can be referenced by any .NET language. It's no longer a matter of VB vs. C# (or any other language) once it's compiled.
If it's not a compiled assembly, but rather a bunch of class files, can it be compiled as its own assembly? That's generally good for keeping things modular. Within a single .NET solution you can have each project use different languages without problem. This module can be a library project written in C# and your site can be a web site (or web application) project written in VB, which references the library project.

Since all these assemblies ultimatly use the same CLR (hence the name Common Language Runtime), they can be used together with no problem.

Related

Easiest way to Implement Scripting to WPF C# Visual Studio 2015

I'd like some help in how to add scripting support to a WPF C# project I'm doing on Visual Studio 2015. One of the things I'd like to do is to be able to change User Control properties within that script. I've being trying Roslyn C#, and I read some stuff about IronPython and PowerShell Tools. But, all that information is not really helping.
So, do you have a simple answer? Like, the easiest way to execute scripts in Visual Studio 2015 C# WPF Application, that are able to change properties of User Controls within the project?
Any help would be appreciated.
Thanks,
Lucas.
T4 templates can help but they are less extensible than lets say Roslyn.
I found a great post on how to easily implement IronPython and how to access User Controls within the scripts in Python, so you can there change different Control properties, and even create them! Here's the link:http://www.codeproject.com/Articles/602112/Scripting-NET-Applications-with-IronPython
I think it should be quite simple to embed powershell or F# interactive in an application and there are lots of examples when we search the web for this. The question is only are we comfortable writing scripts in Powershell and F#?
In my application I provide a C# editor and compile scripts as static methods (which I uses as functions) using the standard .Net compiler API's which are installed with the framework (Microsoft.Csharp, System.CodeDom). This generates a temporary dll and perhaps can not be properly termed a script. The implementation is straight forward and more or less what you expect - I have to provide a way for the users to specify references and namespaces, then generate a source file, compile it to an assembly and then call members of the static class. This above technique works well because the application has extension points that are well adapted to user defined code with a certain well defined structure (you might have similar well defined needs and perhaps full scripting is not necessary).
On the other hand, I am considering providing a "super user" console where the application can be driven by script, and in this case, I will use either Powershell or F-sharp interactive (I'm leaning towards F#). I don't think the implementation is particularly complex, but these are things that can easily bring the application down or corrupt application data if it is not well controlled - and it is unlikely that my users will profit from scripting as it is a programmer environment.

Using multiple programming languages in a ASP.net Web Application

I've a Web Application project that uses VB as programming language. I want to keep using this language due I've more experience and also it works better with IntelliSense.
Anyway, now I am using Open XML SDK 2.0 Productivity Tool for generate code for the generation of a OOXML document through a given template, and this tool only generates C# code.
With the aim to mix both languages in my project, I had followed this tuto but when I try to add a new C# class, the only language that appears in the list is Visual Basic as you can see in the attached screenshot.
Does anyone know how I can fix this?
Thank you very much
This is not possible; you cannot mix C# with VB.NET in the same project. When .NET compiles an assembly it can use only one compiler to do so.
What you can do, is have a solution with multiple projects (for instance one Web app and several class libraries), and then you can have each class library in the language of your choice.
As others have said in general you cannot do this. The one exception is a web site project.
There is a difference between a web site project and a web application. Mainly in the way they are compiled. A web site is what the tutorial you linked talks about (note the way they say to create it via new website rather than new project and choosing web app) whereas you say above that you have a web application.
You have the option of either changing to a web site or using supplemental projects as others have suggested.
Mixing languages is not what I would go for.
Add a new (DLL)project to your solution that uses C# and handles your XML.
Reference that C# project from your VB-app and call into it.
Clean and easy.
You need to install Visual Studio for c#. It looks your current installed language is vb.
Here is link for downloading vs:
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express
About using vb and c# code in same solution, its possible as long they are separated to different projects.

Is it wrong to add razor (cshtml) and c# projects to a Visual Studio vb website?

I'm creating a new website in Visual Studio 2010 Ultimate / vb and I want to add things to it, to jazz it up. So I'm adding new projects from the VS Team Services templates. It seems there are many more projects in c# than vb. I'm trying to figure out if it's okay to add a c# and razor project to a vb website, because if not, I'll start the website in c#.
Will mixing languages between projects within your website present issues in the future when it comes time to build and publish? Or is it recommended I begin the website in c# (since there are more projects within VS) and keep the languages the same throughout the website? Thanks!
There is no issue in doing till you keep the projects separate. That is you should not write one controller in VB and another controller in C# in same project.
As far as i know there is no problem in keeping different projects in same solution
I think that is a great approach when you have a diverse skillset in your team, since some languages are better suited for specific tasks than others.
There sould be no problems with having different languages used in one solution (although you should use one langugage per project) since everything will be compiled into MSIL (some details can be found here).
A similar question was asked: Can you mix .net languages within a single project?
To add a bit more, I would never suggest anyone to mix two different languages. If you start with a solution with one programming languages, continue with that programming language even when you add new projects to that solution. There is nothing worse than having to switch your way of thinking from one language to another. We had a project at my former company where one project was C# and the other two were VB.NET. Maintaining those projects was a nightmare, not to mention a lot of subtle bugs in the way VB.NET and C# evaluate things like if statement.
My suggestion to you is choose one language and stick to it on your project. If you want to improve your knowledge in another language, work with that language, etc.

Why are Visual Studio projects restricted to a single language?

This is question is inspired by the question: In what areas does F# make "absolute no sense in using"?
In theory, it should be possible to use any .NET supported language in a single project. Since every thing should be compiled into IL code, then linked into a single assembly.
Some benefits would include the ability to use say F# for one class, where F# is more suited to implement it's function, and C# for another.
Is there some technical limitation I'm overlooking that prevents this sort of setup?
A project is restricted to a single language because, under the hood, a project is not much more than an MSBuild script which calls one of the command-line compilers to produce an assembly from the various source code files "contained" in the project folder. There is a different compiler for each language (CSC.exe is for example the one for C#), and what each project has to do to turn its "contained" source code into an assembly differs with each language.
To allow multiple languages to be compiled into a single assembly, the project would basically have to produce an assembly for each language, then IL-Merge them. This is costly, requires complex automation and project file code generation, and in most circumstances it's a pretty fringe need, so the VS team simply didn't build it in.
While projects are restricted to a single language, a solution is not... and solutions can contain multiple projects.
As others mentioned, a project is a stand-alone unit that is compiled by a single compiler.
I hear the question about including e.g. one F# type in a larger C# project quite often, so I'll add some details from the F# specific point of view. There are quite a few technical problems that would make it really difficult to mix F# and C# in one project:
C# compiler sees all classes, while F# type declarations are order-dependent. I'm not sure how would you even specify what types should the F# code see at which point.
F# compiler needs to know how declarations are used in order to infer types. Would it also get usage information from the C# compiler?
The two compilers use different representation of types. When compiling, there are no System.Type information, so how would they share the information? (Both of them would need to agree on some common interface that allows them to include language-specific information - and the information may be also incomplete).
I think this is enough to explain that doing this is not just a feature that may or may not be done depending on the schedule. It is actually an interesting research problem.
For what it's worth, it's possible to have ASP.NET projects that use C# and VB.NET (or anything else, you define the compilers in web.config), just in different files.
All code files are processed by a single compiler. That's why a project can only contain a single language.
Mixing languages may not make much sense either, since each language generates it's own IL.
This of course doesn't restrict you form having multiple projects from different langauges in the same solution, since each project is compiled independently
Consider using ILMerge if you want to maintain a single .exe or .dll built by a number of different compilers.
Technically, you can mix languages in a single project, if one (or more) of those languages are scripting languages. See How to use Microsoft.Scripting.Hosting? for more details.
I know this isn't what you were talking about, but it's a little fun fact if you weren't aware.
The project file is nothing but an elevated list of command line parameters to the relevant compiler. A file with the extension of .csproj contains the parameters for a C# compiler, .vbproj for the VB.NET compiler and so on.
You can however create two or more projects in the same solution file, one for each language and then link them together in one exe file using ILMerge.

Converting a C# WinForms application to an assembly?

I have a C# WinForms application which is running fine. Now I want to convert it to an assembly and use it in an ASP.NET Web application.
How do I choose which DLLs to reference?
It depends on which classes and methods you want to use. If they are in the exe, you can simply rename a.exe to a.dll. Then a.dll can be added as reference, and you can consume the public classes.
Noticeably that many things used in WinForms applications are not feasible or applicable to ASP.NET applications and can lead to serious performance issues. This is because WinForms and ASP.NET have different process models. Pay attention to that before it bites you.
You know ASP.NET allows you to use C# code right? Sounds like you just need to reconsider the GUI portion of the app for web usage and then you should be set.
This may or may not be easy. But if you wrote your code with an adequate separation of user interface and actual doing (or business) logic, then it should be easy enough.
Rename the yourapp.EXE to yourapp.DLL and it as reference to any .Net application be it asp.net web or windows. Anything in compiled form in .Net contains MSIL which is a fair candidated for being referenced from any .net application. Just make sure the application also references dependant assemblies and libraries used by the EXE. Like, System.Windows.Forms, etc..

Categories

Resources