Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing VB.net code with c# code

Tags:

c#

.net

vb.net

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 .

like image 892
Night Walker Avatar asked Jan 22 '10 14:01

Night Walker


4 Answers

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.

like image 119
David M Avatar answered Oct 19 '22 19:10

David M


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.

like image 45
MarkJ Avatar answered Oct 19 '22 18:10

MarkJ


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.

like image 23
Raj More Avatar answered Oct 19 '22 19:10

Raj More


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.

like image 26
sashaeve Avatar answered Oct 19 '22 19:10

sashaeve