The .NET Framework is language-independent. We can develop many languages targeting the .NET Framework, such as C#, C++, F#, Visual Basic and Windows PowerShell.
In simple words, a function, class or anything written in one language can be easily used in another language. (For example a function, class or anything written in C# can be easily used in VB.NET).
We're using 2 languages for our example, VB .Net and C#.
Let's look at an example.
What we'll do is, we'll create a Class Library file in C# (in other words a DLL file) and will use that DLL file in a Visual Basic application.
Let's start. First create a Class Library Project in C#.
Here we're going to create a “CalculatorClassLibrary” that contains some public functions, so that they're accessible by an end application.
Create a new class called Calculator with its functions.
Calculator.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace CalculatorClassLibrary
- {
- public class Calculator
- {
- public static double Addition(double number1, double number2)
- {
- return number1 + number2;
- }
-
- public static double Subtraction(double number1, double number2)
- {
- return number1 - number2;
- }
-
- public static double Multiplication(double number1, double number2)
- {
- return number1 * number2;
- }
- public static double Division(double number1, double number2)
- {
- double ans = 0;
- if (number2 == 0)
- {
- ans = 0;
- }
- else
- {
- ans = number1 / number2;
- }
- return ans;
- }
- }
- }
Now Press F6 to build our class Library.
Once our Class Library has been successfully built, right-click on Project in Solution Explorer and select “Open Folder in Windows Explorer”, in our bin\Debug folder we'll get out DLL file as shown below.
Now add a New Project by clicking on the Solution as shown below.
Now we'll create a Visual Basic Project to do Language Interoperability.
In the Add New Project window, go to Other Language and select Visual Basic.
Select Console Application and give it a proper name.
Now what we'll do here is, we'll use that Calculator.dll file to do operations.
Add “CalculatorClassLibrary.dll” into your reference as shown below.
Go to the bin\Debug folder of your Class Library and select the DLL file as shown below.
Like C#, Visual Basic allows you to list each namespace used within the current file.
Visual Basic offers the Imports keyword rather than C#'s using keyword. Add the following Imports statement within the Module.vb code file.
Module1.vb
- Imports System
- Imports CalculatorClassLibrary
-
- Module Module1
-
- Sub Main()
- Dim addition As Double
- Dim multiplication As Double
- addition = Calculator.Addition(10, 20)
- multiplication = Calculator.Multiplication(5, 2)
- Console.WriteLine("Addition is: {0}", addition)
- Console.WriteLine("Multiplication is: {0}", multiplication)
-
- Console.ReadLine()
- End Sub
-
- End Module
As you can see our VB.NET code is showing access to our C# Class Library Methods as shown below.
Everything is now done.
Set the Startup Project to MyVBApplication as the default project.
To do this right-click on your project then select Properties, you'll get the following window where you can set "Single Startup Project" as shown below.
Click Ok and Press F5 and your VB Project will contain a DLL file that is written in C# without any error or warnings. After a successful run you'll get your output.