简体   繁体   中英

Mixing VB.net code with c# code

I have a vb.net solution and I want to add there a new dll files written in c# and use the functionality from the dll, in the code written in vb.net.

I made several uses of it and it seems working all right, but is it a smart thing to do messing vb.net code with c# like I want to do .

And what a dangers of what I am doing ?

Thank a lot for help .

Your DLL is not a C# DLL, it's a .NET DLL. Once compiled, all you have is IL - doesn't matter what language it came from. Should be no problem, unless you encounter one of the odd edge cases where the DLL's interface includes something that is not supported by Visual Basic. But this would be very much an edge case.

The Common Language Specification, or CLS, defines the subset of .NET features that must be supported by a .NET language, and if your DLL is CLS compliant, then you can use it with no problems. If you are confused about the difference between the CLS, CTS, CLR etc, then I found the coverage of it in this book very helpful, though it is primarily a C# book.

Mark your code as CLS compliant, and then the C# compiler will warn you if you do anything that might cause problems when your DLL is called from another .Net language.

Some quotes from MSDN

To fully interact with other objects regardless of the language they were implemented in, objects must expose to callers only those features that are common to all the languages they must interoperate with. For this reason, the Common Language Specification (CLS), which is a set of basic language features needed by many applications, has been defined.

You can mark assemblies, modules, types, and members as CLS-compliant using the CLSCompliantAttribute .

Some CLS-compliant language compilers, such as the C# compiler , enable you to specify that you intend your code to be CLS-compliant. These compilers can check for CLS compliance and let you know when your code uses functionality that is not supported by the CLS.

Also, your organisation will now need C# skills as well as Vb.Net skills. You should probably convince yourself that this is OK, and then convince key decision makers.

You can mix VB and C# code in the same project - I have worked on several projects that have mixed them and have no issues.

You language mix seems to be much more isolated - one solution with multiple C# DLLs and vb project(s).

I don't see many issues with that.

One solution was found here :

However, it is possible to use different languages in a single project. You may need to write command line build file to build the project. In .NET framework SDK, there is one sample on it. You could access it in C:\\Program Files\\Microsoft Visual Studio .NET\\FrameworkSDK\\Samples\\Technologies\\CrossDevLan guage.

This sample demonstrates the use different development languages in a single project. This sample creates two assemblies. The first is a library or DLL assembly that defines a simple base class written in managed extensions for C++. The second assembly is an executable assembly that defines three derived classes written in C#, VB, and IL (Intermediate Language). These types derive from each other and ultimately from the base class written in managed C++. Finally, the executable creates instances of each of the derived types and calls a virtual method for each. The .NET Framework is an environment where various developers can work together seamlessly while developing in their language of choice.

But you can use both VB.NET and C# code inside asp.net application.

You need to create two folders (ex. vbFolder and csFolder) in App_Code folder and write this code in web.config:

<system.web>    
<compilation>    
<CODESUBDIRECTORIES>    
<ADD directoryName="vbFolder" />    
<ADD directoryName="csFolder" />    
</CODESUBDIRECTORIES>    
</compilation>    
</system.web>  

Good explanation is here .

I think biggest danger is to have a developer to know both languages; while C# and VB.NET are similar because they're bound to .NET framework, they have some peculiarities.

You'll find many good C# programmers and many good VB.NET programmers, but can be a little harder to find a good programmer for both languages

Also, take a look into this article: A Manager's Retrospective on the C# versus VB.NET decision as it talks about other items to keep in mind, as developer preferences, language features and recruiting.

Both VB.NET & C# are compiled to MSIL (Microsoft Intermediate Language) not native code and this to complete the full compilation to native (machine) on the end user machine via the exist .NET frame work which is on the end user machine so if it was .NET for operating system x your program should work fine for operating system x and if it was operating system y your application should work fine with OS y, and this is the solution which .NET technology comes with to let the .NET applications operating system in-Dependant.

also there is a COM Marshaler service to support old component (controls) to work with .NET applications so for example you can invoke vb6 control (*.ocx) in C# windows application.

and this is great integration between Microsoft technologies and techniques.

and no need to have developer good in both VB.NET and C#, but any way if you need one, I am here :)

but the question is why I am in both?

this just because I deliver training, So I thought to expand my abilities and I was surprised that they both very near except the syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM